@@ -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