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(tui): Esc interrupts the turn, Ctrl+C clears input / double-tap exits

Salman Qureshi395a70077b3f
diff --git a/src/channels/cli-interrupt.test.ts b/src/channels/cli-interrupt.test.ts
new file mode 100644
index 00000000..73d20f66
--- /dev/null
+++ b/src/channels/cli-interrupt.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, it, vi } from 'vitest';
+import { CLIChannel, INTERRUPTED_MARKER } from './cli.js';
+
+/**
+ * Esc during a running turn must go through the SAME stop routine `/stop`
+ * uses, installed by the boot path — the channel never reaches into the
+ * agent. It writes a one-line marker into the transcript, and is a no-op
+ * when nothing is running (Esc then keeps its exit/back bindings).
+ */
+describe('CLIChannel.interruptTurn', () => {
+ it('is a no-op while idle', () => {
+ const channel = new CLIChannel();
+ const stop = vi.fn();
+ channel.setInterruptHandler(stop);
+ expect(channel.isTurnRunning()).toBe(false);
+ expect(channel.interruptTurn()).toBe(false);
+ expect(stop).not.toHaveBeenCalled();
+ expect(channel.getTuiState().chatMessages.some((m) => m.content === INTERRUPTED_MARKER)).toBe(false);
+ });
+
+ it('calls the installed stop routine once and writes the Interrupted marker', async () => {
+ const channel = new CLIChannel();
+ const stop = vi.fn(async () => 'stopped');
+ channel.setInterruptHandler(stop);
+ await channel.typing();
+ expect(channel.isTurnRunning()).toBe(true);
+ expect(channel.interruptTurn()).toBe(true);
+ expect(stop).toHaveBeenCalledTimes(1);
+ const messages = channel.getTuiState().chatMessages;
+ const marker = messages[messages.length - 1];
+ expect(marker.role).toBe('system');
+ expect(marker.content).toBe(INTERRUPTED_MARKER);
+ expect(marker.content.includes('\n')).toBe(false);
+ });
+
+ it('does nothing without a handler (no agent wired)', async () => {
+ const channel = new CLIChannel();
+ await channel.typing();
+ expect(channel.interruptTurn()).toBe(false);
+ });
+
+ it('a rejecting stop routine never throws into the key handler', async () => {
+ const channel = new CLIChannel();
+ channel.setInterruptHandler(() => Promise.reject(new Error('boom')));
+ await channel.typing();
+ expect(() => channel.interruptTurn()).not.toThrow();
+ await new Promise((r) => setTimeout(r, 0));
+ });
+});
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 75026e31..5182a452 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -406,6 +406,9 @@ export function bracketedPasteSequences(enable: boolean): string {
* lets through only the frame that carries it) — keep them in sync. */
export const TUI_FROZEN_HINT_MARKER = '⏸ frozen';
+/** One-line transcript marker written when Esc interrupts a running turn. */
+export const INTERRUPTED_MARKER = '⏹ Interrupted';
+
export interface TuiState {
mode: AppMode;
viewMode: 'balanced' | 'detailed';
@@ -527,6 +530,9 @@ export class CLIChannel extends BaseChannel {
private inkInstance: ReturnType<typeof render> | null = null;
private inputHandler: ((text: string) => void) | null = null;
private exitHandler: (() => void) | null = null;
+ /** Esc-during-a-turn: installed by the boot path with the agent's stop
+ * routine (the same one `/stop` calls). Null until set. */
+ private interruptHandler: (() => unknown) | null = null;
private permissionResolver: ((value: string | boolean) => void) | null = null;
private menuDepth = 0;
private menuAbortController: AbortController | null = null;
@@ -881,6 +887,47 @@ export class CLIChannel extends BaseChannel {
} catch { /* not a TTY */ }
}
+ /**
+ * Install the routine Esc calls to interrupt the running turn. The boot
+ * path wires it to the agent's `stopAllWork('stopped')` — exactly what
+ * typing `/stop` does — so the channel never reaches into the agent.
+ */
+ setInterruptHandler(handler: (() => unknown) | null): void {
+ this.interruptHandler = handler;
+ }
+
+ /** True while the agent is mid-turn (thinking, running a tool, or reporting a live phase). */
+ isTurnRunning(): boolean {
+ return this.state.isThinking
+ || this.state.toolSteps.some((s) => s.status === 'running')
+ || this.state.liveActivity !== null;
+ }
+
+ /**
+ * Interrupt the running turn (Esc). Writes a one-line "Interrupted"
+ * marker into the transcript and invokes the stop routine. Returns false
+ * (and does nothing) when no turn is running or no handler is installed.
+ */
+ interruptTurn(): boolean {
+ if (!this.interruptHandler || !this.isTurnRunning()) return false;
+ const marker: ChatMessage = {
+ id: `interrupt-${Date.now().toString(36)}`,
+ role: 'system',
+ content: INTERRUPTED_MARKER,
+ timestamp: Date.now(),
+ };
+ this.trimAndSetMessages([...this.state.chatMessages, marker], { liveActivity: null, thinkingPreview: null });
+ try {
+ const result = this.interruptHandler();
+ if (result && typeof (result as Promise<unknown>).catch === 'function') {
+ (result as Promise<unknown>).catch((err) => logger.warn({ err }, 'interrupt handler failed'));
+ }
+ } catch (err) {
+ logger.warn({ err }, 'interrupt handler failed');
+ }
+ return true;
+ }
+
mountTUI(onInput: (text: string) => void, spotifyClient?: any, onExit?: any): void {
this.spotifyClient = spotifyClient ?? null;
this.exitHandler = onExit ?? null;
@@ -1213,6 +1260,7 @@ export class CLIChannel extends BaseChannel {
onExit: () => {
this.scheduleTuiExit();
},
+ onInterrupt: () => { this.interruptTurn(); },
spotifyClient: this.spotifyClient,
}),
{ exitOnCtrlC: false, patchConsole: false, stdin: (this.stdinProxy ?? process.stdin) as unknown as NodeJS.ReadStream, stdout: this.tuiOutput as unknown as NodeJS.WriteStream },
diff --git a/src/index.ts b/src/index.ts
index ccdebc3e..5754fc85 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -2716,6 +2716,8 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {
})) ?? [],
});
bootCli.startStatusPoller(2000);
+ // Esc during a turn: the same stop routine `/stop` runs.
+ bootCli.setInterruptHandler(() => agent.stopAllWork('stopped'));
bootCli.mountTUI((inputText: string) => {
bootCli.sendUserMessage(inputText);
}, spotifyClient, () => {
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index 78bf5b70..908eb7a7 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -14,7 +14,7 @@ import { useTerminalSize as useSharedTerminalSize } from './use-terminal-size.js
import { buildMercuryMessageLines, buildMercuryBrandLines, buildStreamTailLines, parseChunkIndex, splitFinalMessage, splitStreamingMessage, type MercuryTranscriptLine } from './mercury-transcript.js';
import { GENERIC_PHASES, PLANNING_VERBS, lastUserText, pickStatusWord } from './status-word.js';
import { nextTip, rotateTip } from './tips.js';
-import { SLASH_COMMANDS, buildSlashSuggestions, buildSkillSuggestions, skillFillText, shouldSubmitSlash, pushHistoryLine, historyPrev, historyNext, createInputHistoryState, SuggestionList, applyEditKey, isPasteChunk, graphemeAt, nextGraphemeBoundary, expandTabs, type EditorKey, type SkillEntry } from './input-composer.js';
+import { SLASH_COMMANDS, buildSlashSuggestions, buildSkillSuggestions, skillFillText, shouldSubmitSlash, pushHistoryLine, historyPrev, historyNext, createInputHistoryState, SuggestionList, applyEditKey, isPasteChunk, graphemeAt, nextGraphemeBoundary, expandTabs, ctrlCAction, CTRL_C_EXIT_HINT, CTRL_C_EXIT_WINDOW_MS, type EditorKey, type SkillEntry } from './input-composer.js';
import { PLAYER_CONTROLS, formatNowPlaying } from '../spotify/ui.js';
import type { SpotifyClient } from '../spotify/client.js';
import type { SubAgentStatus } from '../types/agent.js';
@@ -78,10 +78,13 @@ export interface TuiAppProps {
onInput: (text: string) => void;
onPermissionResolve: (value: string | boolean) => void;
onExit: () => void;
+ /** Esc during a running turn: interrupt it (the channel routes this to
+ * the same stop path `/stop` uses). Optional for test harnesses. */
+ onInterrupt?: () => void;
spotifyClient?: SpotifyClient | null;
}
-export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyClient }: TuiAppProps) {
+export function TuiApp({ channel, onInput, onPermissionResolve, onExit, onInterrupt, spotifyClient }: TuiAppProps) {
// Single source of render truth: the channel's immutable state snapshots.
// Notifications are scheduled by React's reconciler — no imperative
// re-render path exists, so re-entrant commits are impossible.
@@ -110,6 +113,23 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
// draft snapshotting; see input-composer.tsx).
const [inputHistory, setInputHistory] = React.useState(createInputHistoryState);
const [gitCursor, setGitCursor] = React.useState(0);
+ // Ctrl+C: clear the input when it has text; on an empty input the first
+ // tap arms a 1.5 s window and shows a hint, the second tap exits. A single
+ // tap never exits (and never process.exit()s) — see ctrlCAction().
+ const ctrlCArmedAtRef = React.useRef<number | null>(null);
+ const [ctrlCHint, setCtrlCHint] = React.useState(false);
+ React.useEffect(() => {
+ if (!ctrlCHint) return;
+ const t = setTimeout(() => {
+ ctrlCArmedAtRef.current = null;
+ setCtrlCHint(false);
+ }, CTRL_C_EXIT_WINDOW_MS);
+ return () => clearTimeout(t);
+ }, [ctrlCHint]);
+ // A turn is running while the agent thinks, a tool runs, or a live phase
+ // is reported. Esc interrupts it; Esc's exit/back bindings apply only when
+ // nothing is running.
+ const turnRunning = state.isThinking || state.toolSteps.some((s) => s.status === 'running') || Boolean(state.liveActivity);
// Canonical command list lives in the shared input composer (single source
// of truth for the main TUI AND the attach TUI — see input-composer.tsx).
@@ -290,7 +310,21 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
};
if (ch === '\u0003' || (key.ctrl && ((key as any).name === 'c' || ch?.toLowerCase?.() === 'c'))) {
- onExit();
+ const editable = showInput || state.mode === 'mercury-code';
+ const action = ctrlCAction(editable ? input : '', ctrlCArmedAtRef.current, Date.now());
+ if (action === 'clear') {
+ setInputAndCursor('');
+ ctrlCArmedAtRef.current = null;
+ setCtrlCHint(false);
+ return;
+ }
+ if (action === 'exit') {
+ ctrlCArmedAtRef.current = null;
+ onExit();
+ return;
+ }
+ ctrlCArmedAtRef.current = Date.now();
+ setCtrlCHint(true);
return;
}
@@ -313,8 +347,6 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
const mc = state.mercuryCode;
if (!mc) return;
- if (ch === '\u0003') { onExit(); return; }
-
// Exit confirmation overlay: Esc cancels, y/Enter confirms, Ctrl+D force-quits.
if (mc.exitConfirm) {
if (key.escape) { onInput('/mc exit-cancel'); return; }
@@ -324,6 +356,12 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
return;
}
+ // Esc while a turn runs interrupts it. Esc-Esc to leave Mercury Code
+ // applies only when nothing is running.
+ if (key.escape && turnRunning) {
+ onInterrupt?.();
+ return;
+ }
// Ask agent to exit: arms the confirm overlay.
if (key.escape && state.exitEscArmed) {
onInput('/mc exit-arm');
@@ -530,8 +568,9 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
const focusArea = state.workspace?.focusArea || 'explorer';
const rightPanel = state.workspace?.rightPanel || 'chat';
- // Global workspace shortcuts (always active)
- if (key.escape || (key.ctrl && (ch === 'q' || ch === 'Q'))) {
+ // Global workspace shortcuts (always active). Esc leaves/returns only
+ // when no turn is running — otherwise it falls through to interrupt.
+ if ((key.escape && !turnRunning) || (key.ctrl && (ch === 'q' || ch === 'Q'))) {
// Esc in non-explorer panel returns to explorer; Esc in explorer exits workspace
if (focusArea !== 'explorer') {
onInput('/ws focus explorer');
@@ -652,6 +691,10 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
}
if (key.escape) {
+ if (turnRunning) {
+ onInterrupt?.();
+ return;
+ }
if (state.mode === 'coding') {
onInput('/chat');
}
@@ -785,6 +828,7 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
input={input}
cursorPos={cursorPos}
permIdx={permIdx}
+ ctrlCHint={ctrlCHint}
/>
) : null}
{state.mode === 'spotify' ? <SpotifyBody activeIdx={spotifyIdx} nowPlaying={spotifyNow} status={spotifyStatus} volume={spotifyVolume} albumArtAnsi={spotifyArtAnsi} /> : null}
@@ -812,6 +856,8 @@ export function TuiApp({ channel, onInput, onPermissionResolve, onExit, spotifyC
projectContext={state.projectContext}
botChat={state.botChat}
botsWorking={state.botRoster.filter((b) => b.state === 'running').length}
+ turnRunning={turnRunning}
+ ctrlCHint={ctrlCHint}
/>
)}
{showInput && state.mode !== 'mercury-code' && slashSuggestions.length > 0 && (
@@ -2028,6 +2074,8 @@ function InputBox({
projectContext,
botChat,
botsWorking,
+ turnRunning,
+ ctrlCHint,
}: {
input: string;
cursorPos: number;
@@ -2037,6 +2085,10 @@ function InputBox({
botChat?: { botId: string; botName: string } | null;
/** Bots currently running a turn (live roster, polled every 2s). */
botsWorking?: number;
+ /** A turn is in flight: the hint row offers Esc to interrupt. */
+ turnRunning?: boolean;
+ /** Ctrl+C exit armed: show the second-tap hint. */
+ ctrlCHint?: boolean;
}) {
const inWorkspace = mode === 'workspace';
const inCoding = mode === 'coding' || inWorkspace;
@@ -2092,7 +2144,11 @@ function InputBox({
))}
</Box>
<Box paddingX={1}>
- <Text dimColor>{inWorkspace ? 'Tab switch panels · Ctrl+J chat · Ctrl+P Plan · Ctrl+X Execute · Esc back/exit' : inCoding ? 'Coding chat active. Ctrl+P Plan · Ctrl+X Execute.' : 'Enter send · Ctrl+N newline'}</Text>
+ {ctrlCHint
+ ? <Text color="yellow">{CTRL_C_EXIT_HINT}</Text>
+ : turnRunning
+ ? <Text dimColor>Esc interrupt · Ctrl+C clear input</Text>
+ : <Text dimColor>{inWorkspace ? 'Tab switch panels · Ctrl+J chat · Ctrl+P Plan · Ctrl+X Execute · Esc back/exit' : inCoding ? 'Coding chat active. Ctrl+P Plan · Ctrl+X Execute.' : 'Enter send · Shift+Enter/Ctrl+N newline · Ctrl+C clear · /help keys'}</Text>}
</Box>
</Box>
);
@@ -2542,6 +2598,7 @@ export function MercuryCodeView({
input,
cursorPos,
permIdx,
+ ctrlCHint,
}: {
state: TuiState;
cols: number;
@@ -2549,6 +2606,7 @@ export function MercuryCodeView({
input?: string | undefined;
cursorPos?: number | undefined;
permIdx?: number | undefined;
+ ctrlCHint?: boolean;
}): React.ReactNode {
const mc = state.mercuryCode;
const contentWidth = Math.max(20, cols - 4);
@@ -2684,13 +2742,15 @@ export function MercuryCodeView({
}
// Freeze hint lives in the status bar (not a new row) so the live region's
// height stays constant — a new row would re-introduce scrollback churn.
- const leftHint = state.tuiFrozen
+ const leftHint = ctrlCHint
+ ? CTRL_C_EXIT_HINT
+ : state.tuiFrozen
? `${TUI_FROZEN_HINT_MARKER} — Ctrl+S to resume · /mc resume`
: mc.exitConfirm || state.permissionPrompt
? '' // the prompt / confirm box already shows its own controls
: state.isThinking || state.toolSteps.some((s) => s.status === 'running') || state.subAgents.some((a) => a.status === 'running')
- ? '' // the live feedback block above is showing progress
- : '↵ send · /help';
+ ? 'esc interrupt' // the live feedback block above is showing progress
+ : '↵ send · esc esc exit · /help';
const showHints = finalizedMessages.length === 0 && streamTail.length === 0;
@@ -2713,7 +2773,7 @@ export function MercuryCodeView({
{mc.exitConfirm && <MercuryCodeExitConfirm boxWidth={Math.max(40, cols - 4)} />}
<MercuryCodeInput input={input ?? ''} cursorPos={cursorPos ?? 0} mode={state.programmingMode} boxWidth={Math.max(40, cols - 4)} />
<Box height={1} overflow="hidden" paddingX={2} flexShrink={0}>
- <Text dimColor>{leftHint}</Text>
+ <Text dimColor={!ctrlCHint} color={ctrlCHint ? 'yellow' : undefined}>{leftHint}</Text>
<Spacer />
<Text wrap="truncate-end">
{rightSegs.map((seg, i) => (
diff --git a/src/utils/manual.ts b/src/utils/manual.ts
index 5dd48929..5478da4e 100644
--- a/src/utils/manual.ts
+++ b/src/utils/manual.ts
@@ -219,6 +219,28 @@ export function getManual(): string {
sections.push(` ${chalk.white(cmd.padEnd(16))} ${desc}`);
}
+ sections.push('');
+ sections.push(chalk.bold.white(' Terminal UI keys'));
+ sections.push('');
+
+ const keys = [
+ ['Esc', 'Interrupt the running turn (same as /stop). When idle: Esc Esc leaves Mercury Code; Esc leaves the coding/workspace view'],
+ ['Ctrl+C', 'Clear the input. On an empty input, press twice within 1.5 s to exit — one tap never quits'],
+ ['Enter', 'Send. A paste never sends: multi-line pastes land as multi-line text'],
+ ['Shift+Enter / Ctrl+N', 'Insert a newline'],
+ ['← → / Alt+← Alt+→', 'Move by character / by word (Ctrl+←/→ and Alt+B/F too)'],
+ ['Home / End', 'Start / end of the line (Ctrl+A / Ctrl+E)'],
+ ['Backspace / Delete', 'Delete before / under the cursor (emoji and CJK stay whole)'],
+ ['Ctrl+W / Ctrl+U / Ctrl+K', 'Delete the previous word / to line start / to line end'],
+ ['↑ ↓', 'Input history (persisted in ~/.mercury/history) or move in a suggestion list'],
+ ['Tab', 'Complete the highlighted / # suggestion'],
+ ['Ctrl+S', 'Mercury Code: freeze the screen to scroll and copy; Ctrl+S resumes'],
+ ];
+
+ for (const [key, desc] of keys) {
+ sections.push(` ${chalk.white(key.padEnd(26))} ${desc}`);
+ }
+
sections.push('');
sections.push(chalk.bold.white(' Permissions'));
sections.push('');