Skip to content

The agent that grows with you

fix(tui): a ctrl chord's control byte is not typed text (stray `l` after resume)

finn763addf6949018c
diff --git a/ui-tui/packages/hermes-ink/src/ink/events/input-event-control-byte.test.ts b/ui-tui/packages/hermes-ink/src/ink/events/input-event-control-byte.test.ts
new file mode 100644
index 00000000000..6cc7c83c62d
--- /dev/null
+++ b/ui-tui/packages/hermes-ink/src/ink/events/input-event-control-byte.test.ts
@@ -0,0 +1,83 @@
+import { describe, expect, it } from 'vitest'
+
+import { INITIAL_STATE, parseMultipleKeypresses } from '../parse-keypress.js'
+
+import { InputEvent } from './input-event.js'
+
+// Regression: #115284 — a stray `l` in the TUI composer after a session
+// resume / tab switch / window restore.
+//
+// The dashboard writes the PTY force-redraw byte (Ctrl+L, 0x0c —
+// `hermes_cli/pty_session.py` ``TUI_FORCE_REDRAW``) into the TUI's stdin on
+// every re-attach (`hermes_cli/web_routers/chat_ws.py`:
+// ``session.attach(ws, force_redraw=not _created)``). parse-keypress names a
+// control byte after the letter it encodes (0x0c → 'l') and InputEvent hands
+// that name to ``input`` so bindings can match ctrl+<letter> on the raw byte —
+// so the byte arrived at the composer looking like typed text and landed in the
+// input box as a solitary `l`, prefixed to whatever the user typed next.
+
+const PRINTABLE = /^[ -~\u00a0-\uffff]+$/
+
+/** Mirror the composer's insert gate (ui-tui/src/components/textInput.tsx). */
+const composerWouldInsert = (event: InputEvent): boolean =>
+ !event.isControlByteChord && (event.keypress.isPasted || event.input.length > 0) && PRINTABLE.test(event.input)
+
+function parseOne(bytes: string): InputEvent {
+ const [keys] = parseMultipleKeypresses({ ...INITIAL_STATE }, bytes)
+
+ return new InputEvent(keys[0] as never)
+}
+
+describe('control bytes are chords, not typed text (#115284)', () => {
+ it('does not offer the re-attach redraw byte (Ctrl+L) to inserters', () => {
+ const event = parseOne('\x0c')
+
+ expect(event.key.ctrl).toBe(true)
+ expect(event.input).toBe('l') // binding name Ink derives from the byte
+ expect(composerWouldInsert(event)).toBe(false)
+ expect(event.isControlByteChord).toBe(true)
+ })
+
+ it('still inserts a typed l', () => {
+ const event = parseOne('l')
+
+ expect(event.input).toBe('l')
+ expect(event.isControlByteChord).toBe(false)
+ expect(composerWouldInsert(event)).toBe(true)
+ })
+
+ it('keeps the derived letter on `input` so ctrl chords stay bindable', () => {
+ // Clearing `input` instead of flagging it would break the composer's own
+ // ctrl+a/e/u/k/w/z/y branches and the global ctrl+c/x/o/t pass-through.
+ const chords: Array<[string, string]> = [
+ ['\x01', 'a'],
+ ['\x05', 'e'],
+ ['\x0b', 'k'],
+ ['\x15', 'u'],
+ ['\x17', 'w'],
+ ['\x1a', 'z']
+ ]
+
+ for (const [byte, letter] of chords) {
+ const event = parseOne(byte)
+
+ expect(event.input, JSON.stringify(byte)).toBe(letter)
+ expect(event.key.ctrl, JSON.stringify(byte)).toBe(true)
+ }
+ })
+
+ it('leaves extended-protocol chords and pastes unflagged', () => {
+ // kitty CSI u Ctrl+L is multi-byte, so it is not a bare control byte.
+ const kitty = parseOne('\x1b[108;5u')
+
+ expect(kitty.key.ctrl).toBe(true)
+ expect(kitty.isControlByteChord).toBe(false)
+
+ // Bracketed paste content is text, never a chord.
+ const [keys] = parseMultipleKeypresses({ ...INITIAL_STATE }, '\x1b[200~\x0c\x1b[201~')
+ const pasted = new InputEvent(keys[0] as never)
+
+ expect(pasted.keypress.isPasted).toBe(true)
+ expect(pasted.isControlByteChord).toBe(false)
+ })
+})
diff --git a/ui-tui/packages/hermes-ink/src/ink/events/input-event.ts b/ui-tui/packages/hermes-ink/src/ink/events/input-event.ts
index 36184e29bf8..2abc1eace03 100644
--- a/ui-tui/packages/hermes-ink/src/ink/events/input-event.ts
+++ b/ui-tui/packages/hermes-ink/src/ink/events/input-event.ts
@@ -172,7 +172,34 @@ function parseKey(keypress: ParsedKey): [Key, string] {
return [key, input]
}
+/**
+ * A bare C0 control byte (0x00–0x1f) is a chord, never typed text — but
+ * `parseKeypress` names it after the letter the byte encodes (0x0c → 'l') and
+ * `parseKey` above hands that name to `input`, so bindings can still match
+ * ctrl+<letter> on the raw byte. Text inserters must not read the name as
+ * input: the dashboard writes the PTY force-redraw byte Ctrl+L (0x0c,
+ * `hermes_cli/pty_session.py` TUI_FORCE_REDRAW) into the TUI's stdin on every
+ * re-attach, which typed a solitary `l` into the composer after a session
+ * resume / tab switch / window restore (#115284).
+ *
+ * Only a single-byte sequence qualifies: kitty CSI u / xterm modifyOtherKeys
+ * chords and bracketed pastes keep their sequence, so they are unaffected.
+ */
+function isBareControlByteChord(keypress: ParsedKey): boolean {
+ const sequence = keypress.sequence
+
+ return (
+ keypress.ctrl &&
+ !keypress.isPasted &&
+ typeof sequence === 'string' &&
+ sequence.length === 1 &&
+ sequence.charCodeAt(0) < 0x20
+ )
+}
+
export class InputEvent extends Event {
+ /** `input` is a control byte's binding name, not text the user typed. */
+ readonly isControlByteChord: boolean
readonly keypress: ParsedKey
readonly key: Key
readonly input: string
@@ -181,6 +208,7 @@ export class InputEvent extends Event {
super()
const [key, input] = parseKey(keypress)
+ this.isControlByteChord = isBareControlByteChord(keypress)
this.keypress = keypress
this.key = key
this.input = input
diff --git a/ui-tui/src/components/textInput.tsx b/ui-tui/src/components/textInput.tsx
index aa9d7860d29..ac033f5e73c 100644
--- a/ui-tui/src/components/textInput.tsx
+++ b/ui-tui/src/components/textInput.tsx
@@ -1473,7 +1473,9 @@ export function TextInput({
const delFwd = k.delete || fwdDel.current
const isPrintableInput =
- (event.keypress.isPasted || inp.length > 0) && PRINTABLE.test(inp.replace(BRACKET_PASTE, ''))
+ !event.isControlByteChord &&
+ (event.keypress.isPasted || inp.length > 0) &&
+ PRINTABLE.test(inp.replace(BRACKET_PASTE, ''))
if (!isPrintableInput) {
flushKeyBurst()
@@ -1613,7 +1615,7 @@ export function TextInput({
} else {
;({ cursor: c, value: v } = killToLineEnd(v, c))
}
- } else if (event.keypress.isPasted || inp.length > 0) {
+ } else if (event.keypress.isPasted || (inp.length > 0 && !event.isControlByteChord)) {
const bracketed = event.keypress.isPasted || inp.includes('[200~')
const text = inp.replace(BRACKET_PASTE, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
diff --git a/ui-tui/src/types/hermes-ink.d.ts b/ui-tui/src/types/hermes-ink.d.ts
index 7e7d9b54379..ab33a3a209c 100644
--- a/ui-tui/src/types/hermes-ink.d.ts
+++ b/ui-tui/src/types/hermes-ink.d.ts
@@ -27,6 +27,8 @@ declare module '@hermes/ink' {
export type InputEvent = {
readonly input: string
+ /** `input` is a control byte's binding name, not typed text (#115284). */
+ readonly isControlByteChord: boolean
readonly key: Key
readonly keypress: { readonly isPasted?: boolean; readonly name?: string; readonly raw?: string }
}