ADR 0008: Mode-aware input encoding, Kitty keyboard stacks, and paste safety¶
Status¶
Accepted. 2026-11-21.
Context¶
Driving interactive PTY sessions (tmux, shells, editors, modal TUIs) requires
the outbound half of the terminal contract: encoding structured key, mouse,
focus, and paste events into the exact byte sequences negotiated by the child
application’s active modes (DECCKM ?1, mouse ?1000/?1002/?1003/?1006
/?1016, focus ?1004, bracketed paste ?2004, and the Kitty Keyboard
Protocol CSI u).
Two defects commonly break terminal input encoders:
Shared primary/alternate Kitty keyboard state: Full-screen TUIs push Kitty keyboard flags (
CSI > 1 u) on the?1049alternate screen and crash without popping (CSI < u); sharing one stack corrupts primary-shell key encoding after?1049l. Furthermore, unprefixedCSI uisSCORC(restore cursor) and MUST NOT be confused withCSI ? u/CSI > u/CSI < u/CSI = u.Single-pass
.replace("\x1b[201~", "")bracketed-paste RCE bypass: Sanitizing pasted text withtext.replace("\x1b[201~", "")stitches"\x1b[20\x1b[201~1~; id\r"directly back into"\x1b[201~; id\r", breaking out of bracketed paste and executing arbitrary shell commands! It also misses 8-bitCSI(\x9b201~) and raw\r/\ncommand execution when?2004bracketed paste is disabled.
Decision¶
Provide a pure, zero-dependency libvt.input module integrated with
Terminal.encode(event: InputEvent) -> bytes:
Typed input events (
libvt.input):KeyEvent(key: str, modifiers: KeyMod = KeyMod.NONE, action: KeyAction = KeyAction.PRESS, text: str = "")withKeyEvent.parse("ctrl+shift+p")shorthand.MouseEvent(button: MouseButton, action: MouseAction, row: int, col: int, modifiers: KeyMod = KeyMod.NONE, pixel_x: int = 0, pixel_y: int = 0)using 0-based(row, col)matchingTerminal.FocusEvent(focused: bool)(\x1b[I/\x1b[Owhen?1004is set, elseb"").PasteEvent(text: str).
Independent Primary and Alternate Kitty keyboard stacks:
ScreenSetmaintains separate 8-entry (Limits.max_keyboard_stack)KittyKeyboardStackinstances onprimaryandalternate.CSI > flags upushesflags,CSI < count upopscountentries,CSI = flags ; mode usets/ORs/clearsflags(mode1=set,2=or,3=not), andCSI ? uqueuesb"\x1b[?" + str(flags) + b"u"intodrain_replies(). UnprefixedCSI uremainsSCORC.DECSTRandRISclear the active (or both) keyboard stacks.When active
KittyKeyboardFlagsis non-zero,encode_inputencodes disambiguatedCSI codepoint ; modifiers [: event_type] usequences; otherwise it encodes legacy xterm /DECCKM(\x1bOAvs\x1b[A) bytes.
Fail-closed
encode_paste()(UnsafePasteError):By default (
sanitize=False),encode_paste(text, *, bracketed: bool)raisesUnsafePasteError(LibVtError)iftextcontainsESC(\x1b), 8-bitC1(\x80..\x9f, including\x9b),NUL(\x00),DEL(\x7f), or C0 controls other than\tand (whenbracketed=True)\n/\r. Whenbracketed=False,\rand\nalso raiseUnsafePasteErrorso multi-line paste never executes unbracketed shell lines silently.When callers explicitly opt into
sanitize=True, all C0/C1/ESC/DELcodepoints are stripped character-by-character in a single pass over codepoints (never substring.replace), which cannot re-stitch\x1b[201~.
Alternatives considered¶
Silent
.replace("\x1b[201~", "")sanitization: Rejected due to the nested-substring stitching exploit ("\x1b[20\x1b[201~1~"->"\x1b[201~").Encoding mouse events with 1-based
(x, y)coordinates: Rejected by ADR 0004;MouseEvent(row=0, col=0)stays 0-based(row, col)in Python and adds+ 1on the wire (\x1b[<btn;col+1;row+1M).
Consequences¶
Positive¶
term.encode(KeyEvent.parse("up"))automatically switches between\x1b[A,\x1bOA(?1DECCKM), and KittyCSI ubased on live terminal state.Bracketed paste injection is blocked fail-closed with a dedicated exception.
Tradeoffs¶
Callers pasting multiline text into a shell that has not enabled
?2004must either send explicitKeyEvent("enter")/ rawos.writeor passallow_unbracketed_newlines=Trueintentionally.
Risks¶
Mouse motion floods when
?1003is off. Mitigation:encode_inputchecks active?1000/?1002/?1003/?1006/?1016modes and returnsb""for unsubscribed mouse events.