xonecas.io
64875
Mc

mini-coder / commit f5f4770

fix: align escape handling and update cel-tui

xonecas2026-04-12 f5f477011424d155be4112178c19db7612bf27f3 parent 1243129

8 files changed

README.md+10−10
@@ -75,16 +75,16 @@ Plugins can add more tools, but the core stays intentionally small.
7575
7676 ## Key bindings
7777
78-| Key | Action |
79-| ------------- | ------------------------------------------------- |
80-| `Enter` | Submit message |
81-| `Shift+Enter` | Insert newline |
82-| `Escape` | Interrupt current turn, preserve partial response |
83-| `Tab` | File path autocomplete (or command filter on `/`) |
84-| `Ctrl+R` | Search global raw input history |
85-| `Ctrl+C` | Graceful exit |
86-| `Ctrl+D` | Graceful exit (EOF, when input is empty) |
87-| `:q` | Graceful exit |
78+| Key | Action |
79+| ------------- | ------------------------------------------------------------------------------------------------- |
80+| `Enter` | Submit message |
81+| `Shift+Enter` | Insert newline |
82+| `Escape` | Close the current overlay and refocus input; otherwise interrupt the active turn; otherwise no-op |
83+| `Tab` | File path autocomplete (or command filter on `/`) |
84+| `Ctrl+R` | Search global raw input history |
85+| `Ctrl+C` | Graceful exit |
86+| `Ctrl+D` | Graceful exit (EOF, when input is empty) |
87+| `:q` | Graceful exit |
8888
8989 ## Headless one-shot mode
9090
TODO.md+2−0
@@ -9,6 +9,7 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
99
1010 ## Low priority (deffered until further notice)
1111
12+- [ ] **Many `mc` instances on the same host can cause SQL Busy errors** - Doesn't happen often but I've seen it happen with many mc instances open, which makes sense. We should investigate what we can do to add robustness at the SQL layer.
1213 - [ ] **Prevent `/session` from switching sessions mid-run** (`ui/commands.ts`, `ui/agent.ts`) — selecting a session while a turn is active can desync UI state from the session the loop is still writing to.
1314 - [ ] **Replace custom XML parsing and frontmatter parsing with dedicated dependencies** (`skills.ts`, `prompt.ts`)
1415
@@ -21,5 +22,6 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
2122
2223 ## Future ideas
2324
25+- [ ] cel-tui now supports setting the terminal title text. We should use this to show activity and session information to the user: "Preview of last assistant/user message... - <animation if turn is in flight in sync with divider animation>". Use a simple animation, like a repeating elipsis. we just need to make sure that if the divider is on, the window title is too, they can't be out of sync, that would be confusing.
2426 - [ ] Divider theme plugin — customizable divider animations (scanning pulse, breathing, flowing dots, wave)
2527 - [ ] Backward-compatibility and migration policy for 1.0 (session/app data versioning, explicit migration strategy)
package.json+3−3
@@ -18,9 +18,9 @@
1818 "test": "bun test"
1919 },
2020 "dependencies": {
21- "@cel-tui/components": "^0.6.2",
22- "@cel-tui/core": "^0.6.2",
23- "@cel-tui/types": "^0.6.2",
21+ "@cel-tui/components": "^0.7.1",
22+ "@cel-tui/core": "^0.7.1",
23+ "@cel-tui/types": "^0.7.1",
2424 "@mariozechner/pi-ai": "^0.66.1"
2525 },
2626 "devDependencies": {
spec.md+19−15
@@ -217,10 +217,12 @@ The core runtime. Streaming is the default behavior throughout the turn: user-vi
217217 - Each result becomes a `ToolResultMessage` appended to history and DB (same turn number).
218218 - After all tool results are appended, loop back to step 2 (re-stream with the updated context).
219219
220-5. **Interrupt** — if the user presses `Escape` during streaming:
220+5. **Interrupt** — if the user presses `Escape` during streaming with no overlay open:
221221 - Abort the stream via `AbortSignal`.
222222 - The partial `AssistantMessage` (with `stopReason: "aborted"`) is appended to history and DB as-is. This preserves context so the LLM knows what it was doing when interrupted.
223- - Return to the input prompt. The user can continue the conversation or `/undo` the interrupted turn.
223+ - Return to the input prompt with focus on the input. The user can continue the conversation or `/undo` the interrupted turn.
224+
225+ If an overlay is open instead, that first `Escape` dismisses the overlay, leaves the current draft unchanged, and returns focus to the input without interrupting the turn.
224226
225227 **No step limit** — the loop runs until the model stops (`stopReason: "stop"`) or the user interrupts. There is no maximum number of tool calls per turn.
226228
@@ -694,7 +696,7 @@ Multi-line text input with no prompt prefix — the blinking cursor is the affor
694696 Supports:
695697
696698 - `Tab` for file path autocomplete.
697-- `Ctrl+R` for global input history search. Opens the same centered Select overlay pattern used by interactive commands, populated with previously submitted raw prompt text from all sessions and working directories, newest first. The list is searchable, selecting an entry restores the exact raw prompt into the input for editing (it does not auto-submit), and dismissing the overlay leaves the current draft unchanged.
699+- `Ctrl+R` for global input history search. Opens the same centered Select overlay pattern used by interactive commands, populated with previously submitted raw prompt text from all sessions and working directories, newest first. The list is searchable, selecting an entry restores the exact raw prompt into the input for editing (it does not auto-submit), and dismissing the overlay leaves the current draft unchanged and returns focus to the input.
698700 - `/command` prefix for slash commands.
699701 - `/skill:skill-name` prefix to inject a skill's body into the user message. The `/skill:name` prefix is stripped from the input and the skill's `SKILL.md` body is prepended to the user message content. The rest of the input becomes the user's instruction. Example: `/skill:code-review check the auth module` sends the code-review skill body + "check the auth module" as the user message.
700702 - Image embedding: if we autocomplete a file path ending in `.png`, `.jpg`, `.jpeg`, `.gif`, or `.webp`, and the file exists, it is embedded as `ImageContent` in the user message (base64-encoded). Only when the current model supports image input (`Model.input` includes `"image"`). If the model doesn't support images, or the file doesn't exist, or the input contains other text, the path is sent as plain text. This is intentionally simple — no inline detection within sentences.
@@ -718,18 +720,20 @@ add tests for undo_
718720
719721 ### Key bindings
720722
721-| Key | Context | Action |
722-| ------------- | ------------- | ------------------------------------------------- |
723-| `Enter` | Input focused | Submit message |
724-| `Shift+Enter` | Input focused | Insert newline |
725-| `Escape` | Agent working | Interrupt current turn, preserve partial response |
726-| `Tab` | Input focused | File path autocomplete |
727-| `Ctrl+R` | Input focused | Search global raw input history |
728-| `Ctrl+C` | Any | Graceful exit |
729-| `Ctrl+D` | Input empty | Graceful exit (EOF) |
730-| `:q` | Input focused | Graceful exit |
731-| `Ctrl+Z` | Any | Suspend/background process |
732-| Mouse wheel | Log area | Scroll conversation history |
723+| Key | Context | Action |
724+| ------------- | ------------------------- | -------------------------------------------------------------------------------- |
725+| `Enter` | Input focused | Submit message |
726+| `Shift+Enter` | Input focused | Insert newline |
727+| `Escape` | Overlay open | Dismiss overlay, leave the draft unchanged, and return focus to the input |
728+| `Escape` | Agent working, no overlay | Interrupt current turn, preserve partial response, and return focus to the input |
729+| `Escape` | Idle, no overlay | No action |
730+| `Tab` | Input focused | File path autocomplete |
731+| `Ctrl+R` | Input focused | Search global raw input history |
732+| `Ctrl+C` | Any | Graceful exit |
733+| `Ctrl+D` | Input empty | Graceful exit (EOF) |
734+| `:q` | Input focused | Graceful exit |
735+| `Ctrl+Z` | Any | Suspend/background process |
736+| Mouse wheel | Log area | Scroll conversation history |
733737
734738 ### Commands
735739
src/ui.test.ts+115−6
@@ -182,6 +182,19 @@ async function renderLayoutRows(
182182 return snapshot;
183183 }
184184
185+function startUiViewport(
186+ state: AppState,
187+ terminal: MockTerminal,
188+ controller: InputController,
189+): void {
190+ cel.init(terminal);
191+ cel.viewport(() => {
192+ const base = renderBaseLayout(state, terminal.columns, controller);
193+ const overlay = renderActiveOverlay(state);
194+ return overlay ? [base, overlay] : base;
195+ });
196+}
197+
185198 const tempDirs: string[] = [];
186199
187200 function createTempDir(): string {
@@ -1633,7 +1646,7 @@ describe("ui rendering", () => {
16331646 }
16341647 });
16351648
1636- test("Escape while running blurs the input first and a second Escape aborts", () => {
1649+ test("Escape while running aborts immediately and refocuses the input", () => {
16371650 const state = createTestState();
16381651 state.running = true;
16391652 state.abortController = new AbortController();
@@ -1643,19 +1656,116 @@ describe("ui rendering", () => {
16431656 const base = expectVStack(renderBaseLayout(state, 80, controller));
16441657
16451658 controller.onBlur();
1646-
1647- const input = expectTextInput(renderInputArea(state.theme, controller));
1648- expect(input.props.focused).toBe(false);
1659+ expect(
1660+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1661+ ).toBe(false);
16491662 expect(state.abortController.signal.aborted).toBe(false);
16501663
16511664 base.props.onKeyPress?.("escape");
16521665 expect(state.abortController.signal.aborted).toBe(true);
1666+ expect(
1667+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1668+ ).toBe(true);
1669+ } finally {
1670+ state.db.close();
1671+ }
1672+ });
1673+
1674+ test("running Escape aborts immediately while the input is focused", async () => {
1675+ const state = createTestState();
1676+ state.running = true;
1677+ state.abortController = new AbortController();
1678+ const terminal = new MockTerminal(80, 20);
1679+
1680+ try {
1681+ const controller = createInputController(state);
1682+ startUiViewport(state, terminal, controller);
1683+ await waitForCelRender();
1684+
1685+ expect(
1686+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1687+ ).toBe(true);
1688+
1689+ terminal.sendInput("\x1b");
1690+ await waitForCelRender();
1691+
1692+ expect(state.abortController.signal.aborted).toBe(true);
1693+ expect(
1694+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1695+ ).toBe(true);
1696+ } finally {
1697+ cel.stop();
1698+ state.db.close();
1699+ }
1700+ });
1701+
1702+ test("running Escape dismisses a focused overlay and returns focus to the input", async () => {
1703+ const state = createTestState();
1704+ state.running = true;
1705+ state.abortController = new AbortController();
1706+ const terminal = new MockTerminal(80, 20);
1707+
1708+ try {
1709+ appendPromptHistory(state.db, {
1710+ text: "saved prompt",
1711+ cwd: state.cwd,
1712+ });
1713+
1714+ const controller = createInputController(state);
1715+ startUiViewport(state, terminal, controller);
1716+ await waitForCelRender();
1717+
1718+ terminal.sendInput("\x12");
1719+ await waitForCelRender();
1720+
1721+ expect(renderActiveOverlay(state)).not.toBeNull();
1722+ expect(state.abortController.signal.aborted).toBe(false);
1723+ expect(
1724+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1725+ ).toBe(false);
1726+
1727+ terminal.sendInput("\x1b");
1728+ await waitForCelRender();
1729+
1730+ expect(renderActiveOverlay(state)).toBeNull();
1731+ expect(state.abortController.signal.aborted).toBe(false);
1732+ expect(
1733+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1734+ ).toBe(true);
16531735 } finally {
1736+ cel.stop();
16541737 state.db.close();
16551738 }
16561739 });
16571740
1658- test("Escape aborts a running shell tool once the input is blurred", async () => {
1741+ test("Escape while idle does nothing", async () => {
1742+ const state = createTestState();
1743+ const terminal = new MockTerminal(80, 20);
1744+
1745+ try {
1746+ const controller = createInputController(state);
1747+ startUiViewport(state, terminal, controller);
1748+ await waitForCelRender();
1749+
1750+ expect(
1751+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1752+ ).toBe(true);
1753+
1754+ terminal.sendInput("\x1b");
1755+ await waitForCelRender();
1756+
1757+ expect(state.abortController).toBeNull();
1758+ expect(renderActiveOverlay(state)).toBeNull();
1759+ expect(
1760+ expectTextInput(renderInputArea(state.theme, controller)).props.focused,
1761+ ).toBe(true);
1762+ } finally {
1763+ cel.stop();
1764+ state.db.close();
1765+ }
1766+ });
1767+
1768+ test("Escape aborts a running shell tool immediately", async () => {
16591769 const state = createTestState();
16601770 const faux = registerFauxProvider();
16611771 const cwd = createTempDir();
@@ -1679,7 +1789,6 @@ describe("ui rendering", () => {
16791789 state.messages.some((message) => message.role === "assistant"),
16801790 );
16811791
1682- controller.onBlur();
16831792 const base = expectVStack(renderBaseLayout(state, 80, controller));
16841793 base.props.onKeyPress?.("escape");
16851794
src/ui.ts+7−2
@@ -631,8 +631,13 @@ export function renderBaseLayout(
631631 onSuspend?.();
632632 return;
633633 }
634- if (key === "escape" && state.running) {
635- if (state.abortController) state.abortController.abort();
634+ if (key === "escape") {
635+ if (state.running) {
636+ inputFocused = true;
637+ if (state.abortController) {
638+ state.abortController.abort();
639+ }
640+ }
636641 return;
637642 }
638643 return false;
src/ui/help.test.ts+9−4
@@ -24,7 +24,7 @@ describe("ui/help", () => {
2424 );
2525 });
2626
27- test("buildHelpText includes the current Escape input-focus note", () => {
27+ test("buildHelpText describes the current Escape behavior", () => {
2828 const helpState: HelpRenderState = {
2929 providers: new Map(),
3030 model: null,
@@ -37,8 +37,13 @@ describe("ui/help", () => {
3737
3838 const text = buildHelpText(helpState);
3939
40- expect(text).toContain("Escape blurs the input first");
41- expect(text).toContain("Tab re-focuses the input");
42- expect(text).toContain("Escape again interrupts the current turn");
40+ expect(text).toContain(
41+ "Escape closes the current overlay and returns focus to the input",
42+ );
43+ expect(text).toContain(
44+ "With no overlay open, Escape interrupts the current turn",
45+ );
46+ expect(text).toContain("Otherwise Escape does nothing");
47+ expect(text).not.toContain("Escape blurs the input first");
4348 });
4449 });
src/ui/help.ts+5−3
@@ -79,9 +79,11 @@ export function buildHelpText(state: HelpRenderState): string {
7979 const providerNames = Array.from(state.providers.keys());
8080 lines.push("");
8181 lines.push("Note:");
82- lines.push(" While a turn is running, Escape blurs the input first.");
83- lines.push(" Tab re-focuses the input.");
84- lines.push(" Escape again interrupts the current turn.");
82+ lines.push(
83+ " Escape closes the current overlay and returns focus to the input.",
84+ );
85+ lines.push(" With no overlay open, Escape interrupts the current turn.");
86+ lines.push(" Otherwise Escape does nothing.");
8587
8688 lines.push("");
8789 lines.push(