Skip to content

The agent that grows with you

fix(cli): Ctrl+D leaves a curses menu once raw mode is on, like Esc

Teknium4981c6f0065e
diff --git a/hermes_cli/curses_ui.py b/hermes_cli/curses_ui.py
index 2dc04e5ef2f..27cc8c152b8 100644
--- a/hermes_cli/curses_ui.py
+++ b/hermes_cli/curses_ui.py
@@ -360,7 +360,9 @@ def _enhanced_key_action(codepoint: int, modifier: int = 1) -> str:
# CSI-u encodes Ctrl+C as `c` plus the Ctrl modifier bit; lock-state bits may be added, so
# test the Ctrl bit rather than the canonical value 5.
has_ctrl = bool((max(1, modifier) - 1) & 4)
- return NAV_INTERRUPT if codepoint == 3 or (codepoint in (99, 67) and has_ctrl) else NAV_NONE
+ if codepoint == 3 or (codepoint in (99, 67) and has_ctrl):
+ return NAV_INTERRUPT
+ return NAV_CANCEL if codepoint == 4 or (codepoint in (100, 68) and has_ctrl) else NAV_NONE
def _read_csi_tail(stdscr) -> tuple[str, int | None]:
@@ -410,6 +412,7 @@ def _decode_menu_key(stdscr, key: int) -> str:
plain = {
curses.KEY_UP: NAV_UP, ord("k"): NAV_UP, curses.KEY_DOWN: NAV_DOWN, ord("j"): NAV_DOWN,
curses.KEY_LEFT: NAV_BACK, 3: NAV_INTERRUPT, # 3 = Ctrl+C in raw/cbreak mode
+ 4: NAV_CANCEL, # 4 = Ctrl+D (EOF) in raw mode: leaves the menu, as it does in cooked mode
curses.KEY_ENTER: NAV_SELECT, 10: NAV_SELECT, 13: NAV_SELECT,
ord(" "): NAV_TOGGLE, ord("q"): NAV_CANCEL}
if key in plain:
diff --git a/tests/hermes_cli/test_curses_arrow_keys.py b/tests/hermes_cli/test_curses_arrow_keys.py
index d9e345f64df..241156928fd 100644
--- a/tests/hermes_cli/test_curses_arrow_keys.py
+++ b/tests/hermes_cli/test_curses_arrow_keys.py
@@ -126,6 +126,17 @@ def test_raw_ctrl_c_cancels():
assert read_menu_key(FakeStdscr([3])) == NAV_INTERRUPT
+ [4], # raw/cbreak Ctrl+D
+ [27, ord("["), ord("1"), ord("0"), ord("0"), ord(";"), ord("5"), ord("u")], # CSI-u Ctrl+d
+ [27, ord("["), ord("2"), ord("7"), ord(";"), ord("5"), ord(";"), ord("1"), ord("0"), ord("0"), ord("~")],
+])
+def test_ctrl_d_leaves_a_menu_like_esc(keys):
+ """Ctrl+D is EOF: before raw mode it already aborts a menu, so once raw mode is on it must too.
+ It used to decode to nothing, and a menu (the shared-metrics offer) redrew on every press."""
+ assert read_menu_key(FakeStdscr(keys)) == NAV_CANCEL
+
+
def test_enhanced_enter_selects_filtered_model_while_search_is_active(monkeypatch):
fake = ExhaustingStdscr(
[