mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-29 06:31:32 +00:00
fix(cli): bracketed-paste timeout prevents permanent input freeze (#16263)
When the terminal drops the ESC[201~ end mark during a bracketed paste (terminal race, torn write, SSH glitch, macOS sleep/wake), prompt_toolkit's Vt100Parser keeps buffering all later input in _paste_buffer forever. From the user's perspective, the CLI appears frozen — the only recovery was closing the tab/session. This patch monkey-patches Vt100Parser.feed() so that bracketed-paste mode flushes buffered content as a normal BracketedPaste event after 2 seconds without an end marker, then restores normal parsing. Includes 8 regression tests covering normal paste, timeout recovery, torn end marks, and edge cases. Surgical reapply of PR #27518. Original branch was many months stale (1193 files / 172k LOC of unrelated reverts); the substantive ~77 LOC patch in cli.py plus the new 157-line test file were reapplied onto current main with the contributor's authorship preserved via --author.
This commit is contained in:
parent
8697471419
commit
1b12cd5241
2 changed files with 244 additions and 0 deletions
87
cli.py
87
cli.py
|
|
@ -2360,6 +2360,89 @@ def _strip_leaked_bracketed_paste_wrappers(text: str) -> str:
|
|||
return text
|
||||
|
||||
|
||||
def _apply_bracketed_paste_timeout_patch() -> None:
|
||||
"""Patch prompt_toolkit to recover from torn bracketed-paste sequences.
|
||||
|
||||
prompt_toolkit's ``Vt100Parser.feed()`` buffers all input while waiting
|
||||
for the ESC[201~ end mark. If a terminal drops that end mark (terminal
|
||||
race, torn write, SSH glitch, macOS sleep/wake), input appears frozen
|
||||
forever — the only recovery used to be killing the tab.
|
||||
|
||||
This patch wraps ``Vt100Parser.feed`` so that bracketed-paste mode
|
||||
flushes buffered content as a normal ``BracketedPaste`` event after
|
||||
``_BP_TIMEOUT_S`` seconds without an end marker, then resumes normal
|
||||
parsing. See upstream issue #16263.
|
||||
|
||||
The patch is idempotent — repeated calls are no-ops via the
|
||||
``_hermes_bp_timeout_patched`` sentinel on the module.
|
||||
"""
|
||||
try:
|
||||
import prompt_toolkit.input.vt100_parser as _vt100_mod
|
||||
from prompt_toolkit.keys import Keys as _PtKeys
|
||||
from prompt_toolkit.key_binding.key_processor import KeyPress as _PtKeyPress
|
||||
|
||||
if getattr(_vt100_mod, "_hermes_bp_timeout_patched", False):
|
||||
return
|
||||
|
||||
_BP_TIMEOUT_S = 2.0 # max time to wait for ESC[201~ before flushing
|
||||
|
||||
def _patched_vt100_feed(self_parser, data: str) -> None:
|
||||
if self_parser._in_bracketed_paste:
|
||||
self_parser._paste_buffer += data
|
||||
end_mark = "\x1b[201~"
|
||||
|
||||
if end_mark in self_parser._paste_buffer:
|
||||
end_index = self_parser._paste_buffer.index(end_mark)
|
||||
paste_content = self_parser._paste_buffer[:end_index]
|
||||
self_parser.feed_key_callback(
|
||||
_PtKeyPress(_PtKeys.BracketedPaste, paste_content)
|
||||
)
|
||||
self_parser._in_bracketed_paste = False
|
||||
remaining = self_parser._paste_buffer[
|
||||
end_index + len(end_mark):
|
||||
]
|
||||
self_parser._paste_buffer = ""
|
||||
self_parser._hermes_bp_start = None
|
||||
if remaining:
|
||||
_patched_vt100_feed(self_parser, remaining)
|
||||
else:
|
||||
bp_start = getattr(self_parser, "_hermes_bp_start", None)
|
||||
now = time.monotonic()
|
||||
if bp_start is None:
|
||||
self_parser._hermes_bp_start = now
|
||||
elif now - bp_start > _BP_TIMEOUT_S:
|
||||
paste_content = self_parser._paste_buffer
|
||||
self_parser._in_bracketed_paste = False
|
||||
self_parser._paste_buffer = ""
|
||||
self_parser._hermes_bp_start = None
|
||||
if paste_content:
|
||||
self_parser.feed_key_callback(
|
||||
_PtKeyPress(_PtKeys.BracketedPaste, paste_content)
|
||||
)
|
||||
logger.warning(
|
||||
"Bracketed-paste timeout (%.1fs) — flushed %d bytes "
|
||||
"without end mark. Terminal may have dropped ESC[201~ "
|
||||
"(see #16263).",
|
||||
now - bp_start,
|
||||
len(paste_content),
|
||||
)
|
||||
else:
|
||||
# Normal mode — re-inline prompt_toolkit's normal feed path.
|
||||
# Calling the original feed here would double-buffer after the
|
||||
# bracketed-paste entry transition.
|
||||
for i, c in enumerate(data):
|
||||
if self_parser._in_bracketed_paste:
|
||||
_patched_vt100_feed(self_parser, data[i:])
|
||||
break
|
||||
self_parser._input_parser.send(c)
|
||||
|
||||
_vt100_mod.Vt100Parser.feed = _patched_vt100_feed
|
||||
_vt100_mod._hermes_bp_timeout_patched = True
|
||||
logger.debug("Applied Vt100Parser bracketed-paste timeout patch (#16263)")
|
||||
except Exception as exc: # noqa: BLE001 — defensive: never break startup
|
||||
logger.debug("Bracketed-paste timeout patch skipped: %s", exc)
|
||||
|
||||
|
||||
# Cursor Position Report (CPR / DSR) response, format ``ESC[<row>;<col>R``.
|
||||
# prompt_toolkit's _on_resize() + renderer send ``ESC[6n`` queries to the
|
||||
# terminal; under resize storms or tab switches the terminal's reply can
|
||||
|
|
@ -14151,6 +14234,10 @@ class HermesCLI:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
# Apply bracketed-paste timeout recovery so torn ESC[201~ end marks
|
||||
# don't permanently freeze the input (issue #16263). Idempotent.
|
||||
_apply_bracketed_paste_timeout_patch()
|
||||
|
||||
_original_on_resize = app._on_resize
|
||||
|
||||
def _resize_clear_ghosts():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue