Skip to content

The agent that grows with you

fix(cli,tui): a stalled event loop no longer submits a held Enter ahead of a waiting paste chunk

Teknium669df020d993
diff --git a/hermes_cli/cli_enter_burst.py b/hermes_cli/cli_enter_burst.py
index 3f0f195cc2b..788ba3d4cd3 100644
--- a/hermes_cli/cli_enter_burst.py
+++ b/hermes_cli/cli_enter_burst.py
@@ -25,10 +25,16 @@ BURST_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."""
+ """Record a buffer change; input arriving inside the quiet window keeps a paste burst alive.
+
+ So does input arriving while an Enter is held, however late a stalled loop reads it: it makes
+ that Enter a pasted newline, so the paste's own trailing Enter stays one too.
+ """
now = monotonic()
- if now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S:
+ held = getattr(cli, "_tui_held_enter", None) is not None
+ if held or now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S:
cli._tui_paste_burst_at = now
+ cli._tui_held_enter = None
return now
@@ -52,13 +58,20 @@ def handle_burst_enter(cli, event, buf) -> bool:
return True # inside (or the tail of) a multi-line paste
with_newline = buf.document
changed_at = getattr(cli, "_tui_last_text_change", 0.0)
+ held = cli._tui_held_enter = object()
def _settle() -> None:
+ if cli._tui_held_enter is held:
+ cli._tui_held_enter = None
if not app.is_running or buf.document != with_newline or cli._tui_last_text_change != changed_at:
return # more input arrived (or the buffer was edited): the newline stays
buf.document = before
cli._tui_submit_buffer(event, buf)
app.invalidate()
- loop.call_later(BURST_END_QUIET_S, _settle)
+ # Decide only after one more selector pass has run its reader callbacks: a loop stalled past
+ # the window can reach this timer with the next chunk still unread (a signal handler that
+ # interrupts the selector past its deadline returns no events). asyncio runs a call_soon
+ # queued now ahead of the next pass's readers; the second hop runs after them.
+ loop.call_later(BURST_END_QUIET_S, loop.call_soon, loop.call_soon, _settle)
return True
diff --git a/tests/hermes_cli/test_cli_enter_burst.py b/tests/hermes_cli/test_cli_enter_burst.py
index 581c757b996..3a99a822f96 100644
--- a/tests/hermes_cli/test_cli_enter_burst.py
+++ b/tests/hermes_cli/test_cli_enter_burst.py
@@ -8,7 +8,10 @@ burst clock is a controlled one, advanced explicitly, so a slow runner cannot ch
"""
from __future__ import annotations
+import asyncio
+import os
import queue
+import selectors
from collections import deque
from types import SimpleNamespace
@@ -36,15 +39,18 @@ class _Loop:
self.clock = clock
self.scheduled = []
- def call_later(self, delay, callback):
- self.scheduled.append((delay, callback))
+ def call_later(self, delay, callback, *args):
+ self.scheduled.append((delay, callback, args))
+
+ def call_soon(self, callback, *args): # no input arrives between hops
+ callback(*args)
def run_scheduled(self):
"""Let the quiet window elapse with no input, then fire what was scheduled."""
pending, self.scheduled = self.scheduled, []
- for delay, callback in pending:
+ for delay, callback, args in pending:
self.clock.advance(delay)
- callback()
+ callback(*args)
@pytest.fixture
@@ -98,7 +104,7 @@ def test_text_and_enter_in_one_write_submits_after_the_quiet_window(clock):
# Not an ordinary submit: a provisional newline waits for the quiet window.
assert _submitted(shell) == []
assert buf.text == "/help\n"
- assert [delay for delay, _ in loop.scheduled] == [BURST_END_QUIET_S]
+ assert [delay for delay, *_ in loop.scheduled] == [BURST_END_QUIET_S]
loop.run_scheduled() # input stayed quiet
assert _submitted(shell) == ["/help"]
assert buf.text == ""
@@ -165,3 +171,58 @@ def test_paste_whose_reads_continue_past_the_quiet_window_never_submits(clock):
loop.run_scheduled()
assert buf.text == "one\ntwo continued again end\n"
assert _submitted(shell) == []
+
+
+class _StallingSelector(selectors.DefaultSelector):
+ """One select() returns no events after the loop stalled past its deadline, as CPython does
+ when a signal handler interrupts the wait and the deadline has passed by the time it returns."""
+
+ stall = None
+
+ def select(self, timeout=None):
+ if self.stall is not None:
+ stall, self.stall = self.stall, None
+ stall()
+ return []
+ return super().select(timeout)
+
+
+def test_chunk_waiting_through_an_event_loop_stall_is_read_before_the_quiet_window_ends(clock):
+ """Under host load the loop can stall past the quiet window while the rest of a paste already
+ waits in stdin, and reach the quiet-window timer before reading it. The waiting text keeps the
+ provisional newline, and the paste's own trailing Enter (read with it) stays a newline too.
+ A real selector loop and pipe, on the controlled clock."""
+ shell, buf, _fake_loop, app = _rig(clock)
+ selector = _StallingSelector()
+ loop = asyncio.SelectorEventLoop(selector)
+ loop.time = clock
+ app.loop = loop
+ read_fd, write_fd = os.pipe()
+ try:
+ def _read(): # what prompt_toolkit's reader does with a read of text + Enter
+ text, enter, _ = os.read(read_fd, 1024).decode().partition("\r")
+ buf.insert_text(text)
+ if enter:
+ _enter(shell, app, more_keys_in_read=False)
+
+ loop.add_reader(read_fd, _read)
+ buf.insert_text("one")
+ _enter(shell, app, more_keys_in_read=False)
+
+ def _stall(): # the window elapses and the next chunk lands meanwhile
+ clock.advance(BURST_END_QUIET_S + 0.15)
+ os.write(write_fd, b"two end\r")
+
+ selector.stall = _stall
+ for advance in (0, BURST_END_QUIET_S): # the stalled passes, then a quiet window later
+ clock.advance(advance)
+ for _ in range(4): # stop() queued first: each run_forever() is one zero-timeout pass
+ loop.call_soon(loop.stop)
+ loop.run_forever()
+ finally:
+ loop.remove_reader(read_fd)
+ loop.close()
+ os.close(read_fd)
+ os.close(write_fd)
+ assert buf.text == "one\ntwo end\n"
+ assert _submitted(shell) == []
diff --git a/ui-tui/packages/hermes-ink/src/ink/app-held-return-stall.test.ts b/ui-tui/packages/hermes-ink/src/ink/app-held-return-stall.test.ts
new file mode 100644
index 00000000000..f7898390ef4
--- /dev/null
+++ b/ui-tui/packages/hermes-ink/src/ink/app-held-return-stall.test.ts
@@ -0,0 +1,90 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import App from './components/App.js'
+import reconciler from './reconciler.js'
+
+// A single-line read ending in CR holds the CR for BURST_END_QUIET_MS. Under
+// host load the event loop can stall past that window while the next paste
+// chunk already waits in the fd: Node runs the timers phase before the poll
+// phase, so the hold timer fires first, and stdin.readableLength is still 0
+// because libuv has not read the bytes yet. The waiting chunk must still win.
+
+const makeStdin = () => {
+ const queue: string[] = []
+
+ return {
+ queue,
+ addListener: vi.fn(),
+ listeners: vi.fn(() => []),
+ read: vi.fn(() => queue.shift() ?? null),
+ // Bytes still in the kernel are invisible here, exactly like a real stall.
+ readableLength: 0
+ }
+}
+
+const noopStream = { isTTY: false, write: () => true } as unknown as NodeJS.WriteStream
+
+const makeApp = (stdin: ReturnType<typeof makeStdin>) => {
+ const app = new App({
+ stdin: stdin as unknown as NodeJS.ReadStream,
+ stdout: noopStream,
+ stderr: noopStream,
+ exitOnCtrlC: false,
+ onExit: vi.fn(),
+ onStdinResume: vi.fn(),
+ children: null as any
+ } as any)
+
+ ;(app as any).rawModeEnabledCount = 1
+
+ return app
+}
+
+describe('App held CR under an event-loop stall', () => {
+ let submitted: string[]
+
+ beforeEach(() => {
+ vi.useFakeTimers()
+ submitted = []
+ vi.spyOn(reconciler, 'discreteUpdates').mockImplementation(((_fn: unknown, _app: unknown, keys: any[]) => {
+ for (const key of keys) {
+ if (key.kind === 'key' && key.name === 'return') {
+ submitted.push(key.sequence)
+ }
+ }
+ }) as any)
+ })
+ afterEach(() => {
+ vi.restoreAllMocks()
+ vi.useRealTimers()
+ })
+
+ it('reads a paste chunk already waiting when the hold timer fires before it', () => {
+ const stdin = makeStdin()
+ const app = makeApp(stdin)
+
+ stdin.queue.push('one\r')
+ ;(app as any).handleReadable()
+ expect((app as any).keyParseState.pendingReturn).toBe(true)
+
+ // Stall: the clock passes the hold window, the timers phase runs the hold
+ // timer, and only then does the poll phase deliver the waiting chunk.
+ vi.advanceTimersToNextTimer()
+ stdin.queue.push('two\r')
+ ;(app as any).handleReadable()
+ vi.runOnlyPendingTimers()
+
+ expect(submitted).toEqual([])
+ })
+
+ it('still submits a held CR when nothing arrives', () => {
+ const stdin = makeStdin()
+ const app = makeApp(stdin)
+
+ stdin.queue.push('/help\r')
+ ;(app as any).handleReadable()
+ vi.runAllTimers()
+
+ expect(submitted).toEqual(['\r'])
+ })
+})
diff --git a/ui-tui/packages/hermes-ink/src/ink/components/App.tsx b/ui-tui/packages/hermes-ink/src/ink/components/App.tsx
index ecf18b8f88d..3781b3998a8 100644
--- a/ui-tui/packages/hermes-ink/src/ink/components/App.tsx
+++ b/ui-tui/packages/hermes-ink/src/ink/components/App.tsx
@@ -181,6 +181,8 @@ export default class App extends PureComponent<Props, State> {
keyParseState = INITIAL_STATE
// Timer for flushing incomplete escape sequences
incompleteEscapeTimer: NodeJS.Timeout | null = null
+ // Check-phase hop between that timer firing and the flush; see flushIncomplete
+ incompleteFlushImmediate: NodeJS.Immediate | null = null
// Timeout durations for incomplete sequences (ms)
readonly NORMAL_TIMEOUT = 50 // Short timeout for regular esc sequences
readonly PASTE_TIMEOUT = 500 // Longer timeout for paste operations
@@ -280,6 +282,11 @@ export default class App extends PureComponent<Props, State> {
this.incompleteEscapeTimer = null
}
+ if (this.incompleteFlushImmediate) {
+ clearImmediate(this.incompleteFlushImmediate)
+ this.incompleteFlushImmediate = null
+ }
+
if (this.mouseWatchdogTimer) {
clearInterval(this.mouseWatchdogTimer)
this.mouseWatchdogTimer = null
@@ -504,11 +511,31 @@ export default class App extends PureComponent<Props, State> {
})
}
- // Helper to flush incomplete escape sequences
+ // Timer callback for incomplete escape sequences, unterminated pastes and a
+ // held CR. Node runs timers before the poll phase, so after an event-loop
+ // stall this fires while the next chunk still sits unread in the fd, where
+ // stdin.readableLength cannot see it; flushing here would submit a held CR
+ // ahead of the paste chunk that follows it. Decide in the check phase
+ // instead, after this turn's poll has read whatever was waiting: any read
+ // replaces keyParseState, which voids this flush (processInput re-arms the
+ // timer when the new state still needs one).
flushIncomplete = (): void => {
- // Clear the timer reference
this.incompleteEscapeTimer = null
+ const state = this.keyParseState
+
+ if (this.incompleteFlushImmediate) {
+ clearImmediate(this.incompleteFlushImmediate)
+ }
+ this.incompleteFlushImmediate = setImmediate(() => {
+ this.incompleteFlushImmediate = null
+
+ if (this.keyParseState === state) {
+ this.flushIfIdle()
+ }
+ })
+ }
+ flushIfIdle = (): void => {
// Only proceed if we have an incomplete escape sequence or an unterminated
// bracketed paste. Missing paste-end markers otherwise leave every later
// keystroke trapped in the paste buffer.