The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli,tui): a paste still arriving keeps its trailing newline
Teknium7745fa7f929c
diff --git a/hermes_cli/cli_enter_burst.py b/hermes_cli/cli_enter_burst.pyindex 7c5a10af510..3f0f195cc2b 100644--- a/hermes_cli/cli_enter_burst.py+++ b/hermes_cli/cli_enter_burst.py@@ -6,7 +6,8 @@ deliver every newline as its own Enter key right behind the text, so an Enter wiA script typing ``text`` + Enter in ONE write (``tmux send-keys 'text' Enter``, a PTY write of``text\\r``) looks identical up to that Enter, except nothing follows it and no other Enter camebefore it. So an Enter with more keys of the same read still queued stays a newline, and so does-the last Enter of a burst that already held one (the tail of a multi-line paste). The lone Enter+the last Enter of a burst that already held one (the tail of a multi-line paste: input never went+quiet for ``BURST_END_QUIET_S`` since that earlier Enter, however long the paste ran). The lone Enterthat ends a burst inserts a provisional newline and submits once input has stayed quiet for``BURST_END_QUIET_S``; any input in that window (the next chunk of a paste split across reads)keeps the newline.@@ -23,6 +24,14 @@ RAPID_INPUT_ENTER_WINDOW_S = 0.05BURST_END_QUIET_S = 0.25+def note_text_change(cli) -> float:+ """Record a buffer change; input arriving inside the quiet window keeps a paste burst alive."""+ now = monotonic()+ if now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S:+ cli._tui_paste_burst_at = now+ return now++def handle_burst_enter(cli, event, buf) -> bool:"""Handle an Enter that arrived mid-burst; False when it is an ordinary (human) Enter.@@ -32,8 +41,8 @@ def handle_burst_enter(cli, event, buf) -> bool:now = monotonic()if now - getattr(cli, "_tui_last_text_change", 0.0) >= RAPID_INPUT_ENTER_WINDOW_S:return False- pasted_newline_before = now - getattr(cli, "_tui_burst_newline_at", 0.0) < BURST_END_QUIET_S- cli._tui_burst_newline_at = now+ pasted_newline_before = now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S+ cli._tui_paste_burst_at = nowbefore = buf.document # the newline lands at the cursor, not necessarily at the endbuf.insert_text("\n")app = event.appdiff --git a/hermes_cli/cli_tui_mixin.py b/hermes_cli/cli_tui_mixin.pyindex f4fa0af2cd7..f156f9c6eda 100644--- a/hermes_cli/cli_tui_mixin.py+++ b/hermes_cli/cli_tui_mixin.py@@ -1723,7 +1723,7 @@ class CLITuiMixin:tick), or the newline count jumped by 4+ (terminals that feed characters individuallybut batch newlines; Alt+Enter adds 1 newline per event so never trips it)."""- self._tui_last_text_change = _enter_burst.monotonic() # same clock as the burst check+ self._tui_last_text_change = _enter_burst.note_text_change(self)from cli import _strip_leaked_bracketed_paste_wrappers, _strip_leaked_terminal_responses_with_metatext = _strip_leaked_bracketed_paste_wrappers(buf.text)text, _had_mouse_reports = _strip_leaked_terminal_responses_with_meta(text)diff --git a/tests/hermes_cli/test_cli_enter_burst.py b/tests/hermes_cli/test_cli_enter_burst.pyindex 0fb4eee181e..581c757b996 100644--- a/tests/hermes_cli/test_cli_enter_burst.py+++ b/tests/hermes_cli/test_cli_enter_burst.py@@ -149,3 +149,19 @@ def test_unbracketed_multiline_paste_keeps_newlines_and_never_submits(clock):loop.run_scheduled()assert buf.text == "chunk one\nchunk two"assert _submitted(shell) == []+++def test_paste_whose_reads_continue_past_the_quiet_window_never_submits(clock):+ """A multi-line paste split into reads 100 ms apart, newlines only in the first and last read:+ the paste is still arriving when the last Enter lands, so it stays a newline even though the+ previous newline is older than the quiet window."""+ shell, buf, loop, app = _rig(clock)+ buf.insert_text("one")+ _enter(shell, app, more_keys_in_read=False)+ for chunk in ("two", " continued", " again", " end"):+ clock.advance(0.1)+ buf.insert_text(chunk)+ _enter(shell, app, more_keys_in_read=False) # the paste's trailing newline+ loop.run_scheduled()+ assert buf.text == "one\ntwo continued again end\n"+ assert _submitted(shell) == []diff --git a/ui-tui/packages/hermes-ink/src/ink/parse-keypress.test.ts b/ui-tui/packages/hermes-ink/src/ink/parse-keypress.test.tsindex 4d8ecef95c8..e79f40683e0 100644--- a/ui-tui/packages/hermes-ink/src/ink/parse-keypress.test.ts+++ b/ui-tui/packages/hermes-ink/src/ink/parse-keypress.test.ts@@ -170,6 +170,26 @@ describe('parseMultipleKeypresses text control splitting', () => {])})+ it('keeps the trailing CR of a paste whose reads keep arriving past the quiet window', () => {+ // 'one\r', 'two', ' continued', ' again', ' end\r' 100 ms apart: the last break is older than+ // BURST_END_QUIET_MS, but input never went quiet, so the trailing CR is still paste content.+ let state = INITIAL_STATE+ const keys = []++ for (const [i, chunk] of ['one\r', 'two', ' continued', ' again', ' end\r'].entries()) {+ const [parsed, next] = parseMultipleKeypresses(state, chunk, 1_000 + i * 100)++ keys.push(...parsed)+ state = next+ }++ const [flushed] = parseMultipleKeypresses(state, null, 1_400 + BURST_END_QUIET_MS)++ keys.push(...flushed)+ expect(keys.some(key => 'name' in key && key.name === 'return')).toBe(false)+ expect(keys.at(-1)).toEqual(expect.objectContaining({ raw: ' end\r' }))+ })+it('keeps the trailing CR of a multi-line non-bracketed paste inside the text token', () => {const [keys] = parseMultipleKeypresses(INITIAL_STATE, 'line one\rline two\r')diff --git a/ui-tui/packages/hermes-ink/src/ink/parse-keypress.ts b/ui-tui/packages/hermes-ink/src/ink/parse-keypress.tsindex a021b6bd895..713a25c0ee7 100644--- a/ui-tui/packages/hermes-ink/src/ink/parse-keypress.ts+++ b/ui-tui/packages/hermes-ink/src/ink/parse-keypress.ts@@ -258,8 +258,9 @@ export type KeyParseState = {pasteBuffer: string// CR held back from the end of a single-line read; see BURST_END_QUIET_MS.pendingReturn?: boolean- // When a read last carried a line break as text (non-bracketed paste).- lastPastedBreakAt?: number+ // Last read of a non-bracketed paste burst: a read that carried a line break+ // as text, or any read that followed one without input going quiet.+ pasteBurstAt?: number// Internal tokenizer instance_tokenizer?: Tokenizer}@@ -274,14 +275,15 @@ export const INITIAL_STATE: KeyParseState = {// Enter write (`tmux send-keys 'text' Enter`, a PTY write of `text\r`) or the// first chunk of a non-bracketed paste split across reads. The CR is held for// this long: a flush with nothing else read turns it into `return` (submit);-// any read inside the window makes it paste content. A read ending in CR within-// this window of another read that carried a pasted line break is the tail of-// that paste and stays content. Mirrors hermes_cli/cli_enter_burst.py.+// any read inside the window makes it paste content. Once a read carried a+// pasted line break, a read ending in CR is the tail of that paste and stays+// content until input has been quiet for this long, however many reads the+// paste spans. Mirrors hermes_cli/cli_enter_burst.py.export const BURST_END_QUIET_MS = 250interface TextBurst {pendingReturn: boolean- lastPastedBreakAt: number+ pasteBurstAt: number}// A CR held from the previous read: a flush (or an escape sequence, which is@@ -295,19 +297,25 @@ function resumeHeldReturn(prevState: KeyParseState, input: string, isFlush: boolreturn isFlush || input.startsWith('\x1b') ? [[parseKeypress('\r')], input] : [[], '\r' + input]}+// Any read inside the quiet window keeps a paste burst alive, so a paste that+// spans many reads ends only once input goes quiet.+function extendPasteBurst(pasteBurstAt: number, isFlush: boolean, now: number): number {+ return !isFlush && now - pasteBurstAt < BURST_END_QUIET_MS ? now : pasteBurstAt+}+// `tailOfRead`: the last token of the read, with no escape sequence buffered.function parseTextToken(text: string, burst: TextBurst, tailOfRead: boolean, now: number): ParsedKey[] {const body = text.slice(0, -1)const singleLineEndingInCr = Boolean(body) && text.endsWith('\r') && !/[\r\n]/.test(body)- if (tailOfRead && singleLineEndingInCr && now - burst.lastPastedBreakAt >= BURST_END_QUIET_MS) {+ if (tailOfRead && singleLineEndingInCr && now - burst.pasteBurstAt >= BURST_END_QUIET_MS) {burst.pendingReturn = truereturn parseTextKeypresses(body)}if (text.length > 1 && /[\r\n]/.test(text)) {- burst.lastPastedBreakAt = now+ burst.pasteBurstAt = now}return parseTextKeypresses(text)@@ -339,7 +347,7 @@ export function parseMultipleKeypresses(const isFlush = input === nullconst [held, inputString] = resumeHeldReturn(prevState, isFlush ? '' : inputToString(input), isFlush)const keys: ParsedInput[] = [...held]- const burst: TextBurst = { pendingReturn: false, lastPastedBreakAt: prevState.lastPastedBreakAt ?? -Infinity }+ const burst: TextBurst = { pendingReturn: false, pasteBurstAt: prevState.pasteBurstAt ?? -Infinity }// Get or create tokenizerconst tokenizer = prevState._tokenizer ?? createTokenizer({ x10Mouse: true, legacyAltEnter: true })@@ -418,7 +426,7 @@ export function parseMultipleKeypresses(incomplete: tokenizer.buffer(),pasteBuffer,pendingReturn: burst.pendingReturn,- lastPastedBreakAt: burst.lastPastedBreakAt,+ pasteBurstAt: extendPasteBurst(burst.pasteBurstAt, isFlush, now),_tokenizer: tokenizer}