Files
hermes-agent/tests/cli/test_modify_other_keys_aliases.py
T
kshitij 03bf85d83b fix(cli): restore Kitty keyboard protocol push and complete the extended-key alias table
Commit 4c34eeb416 fixed dead Ctrl+C by removing the Kitty protocol push
(CSI >1u) from _EXTENDED_ENTER_KEYS_SEQ, keeping only modifyOtherKeys
level 2. That regressed kitty-the-terminal completely: kitty removed
xterm modifyOtherKeys support (kovidgoyal/kitty#4075) and only speaks
its own protocol, so after the removal kitty users lost Shift+Enter and
every other extended key — the CSI >4;2m we still pushed is a no-op
there (kitty even logs a PARSE ERROR for it).

The original reason for removing the push is obsolete: #87511 mapped
CSI-u control sequences, so Ctrl+C as ESC[99;5u now parses to
Keys.ControlC and fires the existing c-c binding. (The kernel-INTR
concern in that commit was moot — prompt_toolkit's raw mode clears
ISIG, so Ctrl+C is always handled by the binding, never the kernel.)

Restore the dual push (CSI >1u + CSI >4;2m), exactly mirroring the Ink
TUI, and complete the alias table for what the kitty disambiguate flag
actually emits — #87511 left real gaps, some of which its PR body
wrongly claimed were covered:

- Esc key: ESC[27u (+ modifiers) — previously leaked '[27u' as text
- Ctrl+Backspace -> backward-kill-word (#78285 was closed on the wrong
  claim that codepoint-127 mapping existed; it did not)
- Shift+Space -> space (#86866's second symptom; the Ctrl+Space
  mapping never covered modifier 2)
- Alt+Enter -> newline tuple; Shift+Tab -> BackTab; Ctrl+Tab -> Tab;
  Alt/Shift+Backspace
- Multi-modifier letters (Shift+Alt 4, Ctrl+Shift 6, Ctrl+Alt 7,
  Ctrl+Alt+Shift 8) normalized onto their Ctrl/Escape-prefix targets,
  both unshifted (kitty) and shifted (mok emitters) codepoints
- Kitty PUA functional keys: keypad -> non-keypad equivalents,
  F13-F24, and Ignore for lock/media/modifier-event keys so they are
  consumed instead of leaking (kitty emits these even in legacy mode)

Also: clear the VT100 parser's prefix cache after installing (stale
answers could misparse), and re-push extended keys after
_recover_terminal_input_modes' reset — the recovery previously popped
both modes mid-session and never re-enabled them, silently killing
Shift+Enter until restart.

Refs #87511, #87074, #56684, #56645, #78285, #86866, #87390.
2026-08-16 15:52:24 +05:30

436 lines
16 KiB
Python

"""Regression tests for issue #87711 — Ctrl+key / Alt+key combos broken
under modifyOtherKeys level 2.
When the CLI pushes ``ESC[>4;2m`` (modifyOtherKeys=2) to supported
terminals so Shift+Enter is distinguishable from Enter, the terminal
re-encodes EVERY Ctrl+key combo as ``ESC[27;5;<codepoint>~`` instead of
the raw control byte (``\\x01`` etc.). prompt_toolkit 3.x only ships a
mapping for ``ESC[27;5;13~`` (Ctrl+Enter = Ctrl+M); all other Ctrl+letter
combos are unmapped and leak as literal text or get swallowed.
``install_modify_other_keys_aliases()`` populates ``ANSI_SEQUENCES`` with
the full set so every Ctrl+combo continues to fire the same key binding
the raw byte would.
"""
from __future__ import annotations
import pytest
from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES
from prompt_toolkit.input.vt100_parser import Vt100Parser
from prompt_toolkit.keys import Keys
from hermes_cli.pt_input_extras import install_modify_other_keys_aliases
@pytest.fixture(autouse=True)
def _ensure_alias_installed():
"""Install the alias for each test, then restore ANSI_SEQUENCES to its
prior state so the hundreds of installed mappings don't leak into
sibling test files."""
from prompt_toolkit.input.ansi_escape_sequences import ANSI_SEQUENCES as _seq
saved = dict(_seq)
install_modify_other_keys_aliases()
yield
_seq.clear()
_seq.update(saved)
# Drop the parser's prefix cache too — it was computed against the
# augmented table and would go stale after the restore above.
from prompt_toolkit.input.vt100_parser import _IS_PREFIX_OF_LONGER_MATCH_CACHE
_IS_PREFIX_OF_LONGER_MATCH_CACHE.clear()
def _parse(byte_seq: str):
"""Feed bytes through prompt_toolkit's VT100 parser and return the
list of KeyPress objects."""
out = []
parser = Vt100Parser(out.append)
for ch in byte_seq:
parser.feed(ch)
parser.flush()
return [kp.key for kp in out]
# ---------------------------------------------------------------------------
# Ctrl+letter: a-z
# ---------------------------------------------------------------------------
CTRL_LETTERS = [chr(c) for c in range(ord('a'), ord('z') + 1)]
@pytest.mark.parametrize("letter", CTRL_LETTERS)
def test_modify_other_keys_ctrl_letter_parses_as_raw_byte(letter):
"""Ctrl+<letter> under modifyOtherKeys must parse identically to the
raw control byte that prompt_toolkit already understands."""
raw_byte = chr(ord(letter) - ord('a') + 1) # Ctrl+a = \x01, etc.
raw_result = _parse(raw_byte)
assert len(raw_result) == 1, f"raw byte {raw_byte!r} should produce 1 keypress"
# modifyOtherKeys format
mok_seq = f"\x1b[27;5;{ord(letter)}~"
mok_result = _parse(mok_seq)
assert mok_result == raw_result, (
f"modifyOtherKeys Ctrl+{letter} ({mok_seq!r}) should parse identically "
f"to raw {raw_byte!r}; got {mok_result!r} vs {raw_result!r}"
)
# CSI-u format
csiu_seq = f"\x1b[{ord(letter)};5u"
csiu_result = _parse(csiu_seq)
assert csiu_result == raw_result, (
f"CSI-u Ctrl+{letter} ({csiu_seq!r}) should parse identically "
f"to raw {raw_byte!r}; got {csiu_result!r} vs {raw_result!r}"
)
@pytest.mark.parametrize("letter", CTRL_LETTERS)
def test_modify_other_keys_ctrl_letter_single_keypress(letter):
"""Each Ctrl+letter sequence must produce exactly one keypress —
a partial match would emit Escape plus literal text."""
for seq in (f"\x1b[27;5;{ord(letter)}~", f"\x1b[{ord(letter)};5u"):
result = _parse(seq)
assert len(result) == 1, (
f"{seq!r} should produce exactly 1 keypress, got {len(result)}: {result!r}"
)
# ---------------------------------------------------------------------------
# Critical individual shortcuts
# ---------------------------------------------------------------------------
def test_ctrl_c_under_modify_other_keys():
"""Ctrl+C must produce Keys.ControlC, not literal text (#56684)."""
assert _parse("\x1b[27;5;99~") == [Keys.ControlC]
assert _parse("\x1b[99;5u") == [Keys.ControlC]
def test_ctrl_a_under_modify_other_keys():
"""Ctrl+A (line start) must still fire."""
assert _parse("\x1b[27;5;97~") == [Keys.ControlA]
assert _parse("\x1b[97;5u") == [Keys.ControlA]
def test_ctrl_e_under_modify_other_keys():
"""Ctrl+E (line end) must still fire."""
assert _parse("\x1b[27;5;101~") == [Keys.ControlE]
assert _parse("\x1b[101;5u") == [Keys.ControlE]
def test_ctrl_u_under_modify_other_keys():
"""Ctrl+U (kill line) must still fire."""
assert _parse("\x1b[27;5;117~") == [Keys.ControlU]
assert _parse("\x1b[117;5u") == [Keys.ControlU]
def test_ctrl_k_under_modify_other_keys():
"""Ctrl+K (kill to end) must still fire."""
assert _parse("\x1b[27;5;107~") == [Keys.ControlK]
assert _parse("\x1b[107;5u") == [Keys.ControlK]
def test_ctrl_r_under_modify_other_keys():
"""Ctrl+R (reverse search) must still fire."""
assert _parse("\x1b[27;5;114~") == [Keys.ControlR]
assert _parse("\x1b[114;5u") == [Keys.ControlR]
def test_ctrl_d_under_modify_other_keys():
"""Ctrl+D (EOF / delete) must still fire."""
assert _parse("\x1b[27;5;100~") == [Keys.ControlD]
assert _parse("\x1b[100;5u") == [Keys.ControlD]
def test_ctrl_w_under_modify_other_keys():
"""Ctrl+W (delete word) must still fire."""
assert _parse("\x1b[27;5;119~") == [Keys.ControlW]
assert _parse("\x1b[119;5u") == [Keys.ControlW]
def test_ctrl_z_under_modify_other_keys():
"""Ctrl+Z (suspend) must still fire."""
assert _parse("\x1b[27;5;122~") == [Keys.ControlZ]
assert _parse("\x1b[122;5u") == [Keys.ControlZ]
def test_ctrl_l_under_modify_other_keys():
"""Ctrl+L (clear screen) must still fire."""
assert _parse("\x1b[27;5;108~") == [Keys.ControlL]
assert _parse("\x1b[108;5u") == [Keys.ControlL]
# ---------------------------------------------------------------------------
# Ctrl+digit: 0-9
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("digit", [str(d) for d in range(10)])
def test_modify_other_keys_ctrl_digit(digit):
"""Ctrl+digit under modifyOtherKeys must map to Keys.Control<digit>."""
codepoint = ord(digit)
expected = getattr(Keys, f"Control{digit}")
mok_seq = f"\x1b[27;5;{codepoint}~"
assert _parse(mok_seq) == [expected], (
f"modifyOtherKeys Ctrl+{digit} ({mok_seq!r}) should parse as {expected}"
)
csiu_seq = f"\x1b[{codepoint};5u"
assert _parse(csiu_seq) == [expected], (
f"CSI-u Ctrl+{digit} ({csiu_seq!r}) should parse as {expected}"
)
# ---------------------------------------------------------------------------
# Ctrl+symbol
# ---------------------------------------------------------------------------
def test_ctrl_left_bracket_under_modify_other_keys():
"""Ctrl+[ = Escape under modifyOtherKeys."""
assert _parse("\x1b[27;5;91~") == _parse("\x1b")
assert _parse("\x1b[91;5u") == _parse("\x1b")
def test_ctrl_backslash_under_modify_other_keys():
"""Ctrl+\\ = ControlBackslash under modifyOtherKeys."""
assert _parse("\x1b[27;5;92~") == [Keys.ControlBackslash]
assert _parse("\x1b[92;5u") == [Keys.ControlBackslash]
def test_ctrl_right_bracket_under_modify_other_keys():
"""Ctrl+] = ControlSquareClose under modifyOtherKeys."""
assert _parse("\x1b[27;5;93~") == [Keys.ControlSquareClose]
assert _parse("\x1b[93;5u") == [Keys.ControlSquareClose]
def test_ctrl_space_under_modify_other_keys():
"""Ctrl+Space = ControlAt (NUL) under modifyOtherKeys."""
assert _parse("\x1b[27;5;32~") == _parse("\x00")
assert _parse("\x1b[32;5u") == _parse("\x00")
# ---------------------------------------------------------------------------
# Alt+letter
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", [chr(c) for c in range(ord('a'), ord('z') + 1)])
def test_modify_other_keys_alt_letter_parses_as_escape_letter(letter):
"""Alt+<letter> under modifyOtherKeys must parse to the same tuple
that a bare ESC+<letter> produces."""
bare_result = _parse(f"\x1b{letter}")
assert len(bare_result) == 2, f"bare ESC+{letter} should produce 2 keypresses"
mok_seq = f"\x1b[27;3;{ord(letter)}~"
mok_result = _parse(mok_seq)
assert mok_result == bare_result, (
f"modifyOtherKeys Alt+{letter} ({mok_seq!r}) should parse identically "
f"to bare ESC+{letter}; got {mok_result!r} vs {bare_result!r}"
)
csiu_seq = f"\x1b[{ord(letter)};3u"
csiu_result = _parse(csiu_seq)
assert csiu_result == bare_result, (
f"CSI-u Alt+{letter} ({csiu_seq!r}) should parse identically "
f"to bare ESC+{letter}; got {csiu_result!r} vs {bare_result!r}"
)
# ---------------------------------------------------------------------------
# Idempotency and non-clobbering
# ---------------------------------------------------------------------------
def test_install_is_idempotent():
install_modify_other_keys_aliases()
assert install_modify_other_keys_aliases() == 0
# ---------------------------------------------------------------------------
# Shift+letter → uppercase
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", [chr(c) for c in range(ord('a'), ord('z') + 1)])
def test_modify_other_keys_shift_letter_produces_uppercase(letter):
"""Shift+<letter> under modifyOtherKeys must produce the uppercase
character, not leak as literal escape text — the 'caps locked' bug."""
upper = letter.upper()
# modifyOtherKeys format
mok_seq = f"\x1b[27;2;{ord(letter)}~"
assert _parse(mok_seq) == [upper], (
f"modifyOtherKeys Shift+{letter} ({mok_seq!r}) should produce '{upper}'"
)
# CSI-u format
csiu_seq = f"\x1b[{ord(letter)};2u"
assert _parse(csiu_seq) == [upper], (
f"CSI-u Shift+{letter} ({csiu_seq!r}) should produce '{upper}'"
)
def test_does_not_clobber_shift_enter_alias():
"""install_modify_other_keys_aliases must not overwrite mappings
installed by install_shift_enter_alias (modifier=2, not 5)."""
from hermes_cli.pt_input_extras import install_shift_enter_alias
install_shift_enter_alias()
assert ANSI_SEQUENCES["\x1b[27;2;13~"] == (Keys.Escape, Keys.ControlM)
assert ANSI_SEQUENCES["\x1b[13;2u"] == (Keys.Escape, Keys.ControlM)
def test_does_not_clobber_ctrl_enter_alias():
"""install_modify_other_keys_aliases must not overwrite mappings
installed by install_ctrl_enter_alias (which maps Ctrl+Enter)."""
from hermes_cli.pt_input_extras import install_ctrl_enter_alias
install_ctrl_enter_alias()
# Ctrl+Enter (modifier=5, codepoint=13) is mapped to (Escape, ControlM)
assert ANSI_SEQUENCES["\x1b[27;5;13~"] == (Keys.Escape, Keys.ControlM)
assert ANSI_SEQUENCES["\x1b[13;5u"] == (Keys.Escape, Keys.ControlM)
def test_ctrl_enter_still_works_under_modify_other_keys():
"""Ctrl+Enter must produce the Alt+Enter newline tuple, not plain Ctrl+M.
This is the install_ctrl_enter_alias behavior — our new function must
not clobber it."""
from hermes_cli.pt_input_extras import install_ctrl_enter_alias
install_ctrl_enter_alias()
install_modify_other_keys_aliases()
alt_enter = _parse("\x1b\r")
ctrl_enter_mok = _parse("\x1b[27;5;13~")
ctrl_enter_csiu = _parse("\x1b[13;5u")
assert ctrl_enter_mok == alt_enter
assert ctrl_enter_csiu == alt_enter
def test_plain_enter_remains_distinct():
"""Plain Enter must keep producing a single keypress (submit), not
the two-key Alt+Enter tuple."""
enter = _parse("\r")
alt_enter = _parse("\x1b\r")
assert enter != alt_enter
assert len(enter) == 1
assert len(alt_enter) == 2
# ---------------------------------------------------------------------------
# Kitty keyboard protocol: the Esc KEY (disambiguate mode, #56684)
# ---------------------------------------------------------------------------
def test_kitty_plain_escape_key():
"""The Esc key under kitty disambiguate mode (ESC[27u) must parse as
Keys.Escape, not leak '[27u' as literal text."""
assert _parse("\x1b[27u") == [Keys.Escape]
@pytest.mark.parametrize("modifier", range(2, 9))
def test_kitty_modified_escape_key(modifier):
"""Modified Esc (Shift+Esc etc.) still behaves as Escape."""
assert _parse(f"\x1b[27;{modifier}u") == [Keys.Escape]
# ---------------------------------------------------------------------------
# Modified Enter / Tab / Backspace / Space
# ---------------------------------------------------------------------------
def test_alt_enter_produces_newline_tuple():
"""Alt+Enter under either protocol must equal bare ESC+CR (newline)."""
alt_enter = _parse("\x1b\r")
assert _parse("\x1b[13;3u") == alt_enter
assert _parse("\x1b[27;3;13~") == alt_enter
def test_shift_tab_produces_backtab():
"""Shift+Tab must behave like the legacy ESC[Z (BackTab)."""
assert _parse("\x1b[9;2u") == [Keys.BackTab]
assert _parse("\x1b[27;2;9~") == [Keys.BackTab]
def test_ctrl_backspace_produces_backward_kill_word():
"""Ctrl+Backspace must produce the (Escape, ControlH) tuple that fires
prompt_toolkit's backward-kill-word — parity with the Ink TUI and
Desktop (#78285)."""
expected = [Keys.Escape, Keys.ControlH]
assert _parse("\x1b[127;5u") == expected
assert _parse("\x1b[27;5;127~") == expected
def test_alt_backspace_produces_backward_kill_word():
"""Alt+Backspace = ESC+DEL = backward-kill-word."""
assert _parse("\x1b[127;3u") == [Keys.Escape, Keys.ControlH]
assert _parse("\x1b[27;3;127~") == [Keys.Escape, Keys.ControlH]
def test_shift_backspace_is_plain_backspace():
assert _parse("\x1b[127;2u") == _parse("\x7f")
def test_shift_space_inserts_space():
"""Shift+Space must insert a space, not leak escape text (#86866)."""
assert _parse("\x1b[32;2u") == [" "]
assert _parse("\x1b[27;2;32~") == [" "]
# ---------------------------------------------------------------------------
# Multi-modifier combos (Ctrl+Shift / Ctrl+Alt / Shift+Alt / Ctrl+Alt+Shift)
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("letter", ["c", "r", "z"])
def test_ctrl_shift_letter_behaves_as_ctrl(letter):
"""Ctrl+Shift+<letter> (modifier 6) must fire the same key as
Ctrl+<letter> — both the unshifted and shifted codepoint variants."""
expected = _parse(chr(ord(letter) & 0x1F))
for cp in (ord(letter), ord(letter) - 32):
assert _parse(f"\x1b[{cp};6u") == expected
assert _parse(f"\x1b[27;6;{cp}~") == expected
def test_ctrl_alt_letter_behaves_as_escape_ctrl():
"""Ctrl+Alt+a (modifier 7) = Escape prefix + ControlA."""
assert _parse("\x1b[97;7u") == [Keys.Escape, Keys.ControlA]
def test_ctrl_alt_shift_letter_behaves_as_escape_ctrl():
"""Ctrl+Alt+Shift+a (modifier 8) = Escape prefix + ControlA."""
assert _parse("\x1b[97;8u") == [Keys.Escape, Keys.ControlA]
def test_shift_alt_letter_behaves_as_escape_upper():
"""Shift+Alt+f (modifier 4) = Escape prefix + 'F'."""
assert _parse("\x1b[102;4u") == [Keys.Escape, "F"]
# ---------------------------------------------------------------------------
# Kitty functional keys (Private Use Area codepoints)
# ---------------------------------------------------------------------------
def test_keypad_enter_behaves_as_enter():
assert _parse("\x1b[57414u") == _parse("\r")
@pytest.mark.parametrize("digit", range(10))
def test_keypad_digits_type_digits(digit):
assert _parse(f"\x1b[{57399 + digit}u") == [str(digit)]
def test_keypad_navigation_maps_to_arrows():
assert _parse("\x1b[57417u") == [Keys.Left]
assert _parse("\x1b[57418u") == [Keys.Right]
assert _parse("\x1b[57419u") == [Keys.Up]
assert _parse("\x1b[57420u") == [Keys.Down]
def test_f13_maps_to_f13():
assert _parse("\x1b[57376u") == [Keys.F13]
@pytest.mark.parametrize("code", [57358, 57428, 57441, 57448])
def test_lock_media_modifier_events_are_consumed(code):
"""Caps Lock, media keys, and bare modifier press events must be
consumed (Keys.Ignore), never leak as literal text."""
result = _parse(f"\x1b[{code}u")
assert result == [Keys.Ignore], f"CSI {code}u leaked: {result!r}"
def test_cmd_backspace_alias_not_clobbered():
"""install_cmd_backspace_alias's super-modifier mappings must survive."""
from hermes_cli.pt_input_extras import install_cmd_backspace_alias
install_cmd_backspace_alias()
install_modify_other_keys_aliases()
assert _parse("\x1b[127;9u") == [Keys.ControlU]