mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-01 01:51:44 +00:00
fix(cli): eliminate ghost status-bar + DSR input leaks from terminal drift
The CLI renders through prompt_toolkit in non-full-screen mode, so every repaint uses the renderer's tracked _cursor_pos.y to cursor_up() + erase before drawing the new frame. Any time that tracked position drifts from terminal reality, redraws stack on top of stale content instead of overwriting it. Four user-visible bugs share this root cause. Fixes: - #5474 (SIGWINCH ghosts): the resize wrapper previously only handled column-shrink reflow. Generalize it to force a full screen-clear (erase_screen + cursor_goto(0,0)) and renderer.reset() on every resize — covers widen, row-shrink, and multiplexer SIGWINCH-less redraws. - #8688 (cmux/tmux tab switch): no SIGWINCH fires on focus regain, so prompt_toolkit has no signal to recover. Add a _force_full_redraw() helper, bound to Ctrl+L (standard bash/zsh/vim convention) and exposed as /redraw. Users can manually clear drift without restarting Hermes. - #14692 (DSR response leaks — ^[[53;1R): resize storms make prompt_toolkit's CSI 6n queries race past the input parser; the terminal's reply ends up as literal input text. Add a sibling of the bracketed-paste sanitizer that strips \x1b[<row>;<col>R and the caret-escape visible form from paste text, buffer text-filter, and the input-processing loop. The idle-redraw removal (#12641) is in the preceding commit from @foxion37 — keeping them as separate commits preserves attribution.
This commit is contained in:
parent
5e92b67807
commit
bb00b783fb
5 changed files with 236 additions and 25 deletions
73
tests/cli/test_cli_force_redraw.py
Normal file
73
tests/cli/test_cli_force_redraw.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
"""Tests for CLI redraw helpers used to recover from terminal buffer drift.
|
||||
|
||||
Covers:
|
||||
- _force_full_redraw (#8688 cmux tab switch, /redraw, Ctrl+L)
|
||||
- the resize handler we install over prompt_toolkit's _on_resize (#5474)
|
||||
|
||||
Both behaviors are exercised against fake prompt_toolkit renderer/output
|
||||
objects — we're asserting the escape sequences the CLI sends, not that
|
||||
the terminal physically repainted.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from cli import HermesCLI
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bare_cli():
|
||||
"""A HermesCLI with no __init__ — we only exercise the redraw helper."""
|
||||
cli = object.__new__(HermesCLI)
|
||||
return cli
|
||||
|
||||
|
||||
class TestForceFullRedraw:
|
||||
def test_no_app_is_safe(self, bare_cli):
|
||||
# _force_full_redraw must be a no-op when the TUI isn't running.
|
||||
bare_cli._app = None
|
||||
bare_cli._force_full_redraw() # must not raise
|
||||
|
||||
def test_missing_app_attr_is_safe(self, bare_cli):
|
||||
# Simulate HermesCLI before the TUI has ever been constructed.
|
||||
bare_cli._force_full_redraw() # must not raise
|
||||
|
||||
def test_sends_full_clear_and_invalidates(self, bare_cli):
|
||||
app = MagicMock()
|
||||
out = app.renderer.output
|
||||
bare_cli._app = app
|
||||
|
||||
bare_cli._force_full_redraw()
|
||||
|
||||
# Must erase screen, home cursor, and flush — in that order.
|
||||
out.reset_attributes.assert_called_once()
|
||||
out.erase_screen.assert_called_once()
|
||||
out.cursor_goto.assert_called_once_with(0, 0)
|
||||
out.flush.assert_called_once()
|
||||
|
||||
# Must reset prompt_toolkit's tracked screen/cursor state so the
|
||||
# next incremental redraw starts from a clean (0, 0) origin.
|
||||
app.renderer.reset.assert_called_once_with(leave_alternate_screen=False)
|
||||
|
||||
# Must schedule a repaint.
|
||||
app.invalidate.assert_called_once()
|
||||
|
||||
def test_swallows_renderer_exceptions(self, bare_cli):
|
||||
# If the renderer blows up for any reason, the helper must not
|
||||
# propagate — otherwise a stray Ctrl+L would crash the CLI.
|
||||
app = MagicMock()
|
||||
app.renderer.output.erase_screen.side_effect = RuntimeError("boom")
|
||||
bare_cli._app = app
|
||||
|
||||
bare_cli._force_full_redraw() # must not raise
|
||||
|
||||
# invalidate() is still attempted after a renderer failure.
|
||||
app.invalidate.assert_called_once()
|
||||
|
||||
def test_swallows_invalidate_exceptions(self, bare_cli):
|
||||
app = MagicMock()
|
||||
app.invalidate.side_effect = RuntimeError("boom")
|
||||
bare_cli._app = app
|
||||
|
||||
bare_cli._force_full_redraw() # must not raise
|
||||
57
tests/cli/test_cli_terminal_response_sanitizer.py
Normal file
57
tests/cli/test_cli_terminal_response_sanitizer.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
"""Tests for defensive terminal control-response stripping in the CLI.
|
||||
|
||||
Covers Cursor Position Report (CPR / DSR) responses that occasionally
|
||||
leak into the input buffer after terminal resize storms or multiplexer
|
||||
tab switches — see issue #14692.
|
||||
"""
|
||||
|
||||
from cli import _strip_leaked_terminal_responses
|
||||
|
||||
|
||||
class TestStripLeakedTerminalResponses:
|
||||
def test_plain_text_unchanged(self):
|
||||
text = "hello world"
|
||||
assert _strip_leaked_terminal_responses(text) == text
|
||||
|
||||
def test_empty_text(self):
|
||||
assert _strip_leaked_terminal_responses("") == ""
|
||||
|
||||
def test_strips_canonical_dsr_response(self):
|
||||
# Reports from issue #14692
|
||||
text = "\x1b[53;1R"
|
||||
assert _strip_leaked_terminal_responses(text) == ""
|
||||
|
||||
def test_strips_dsr_response_in_middle_of_text(self):
|
||||
text = "hello\x1b[53;1Rworld"
|
||||
assert _strip_leaked_terminal_responses(text) == "helloworld"
|
||||
|
||||
def test_strips_multiple_dsr_responses(self):
|
||||
text = "a\x1b[53;1Rb\x1b[51;1Rc\x1b[50;9Rd"
|
||||
assert _strip_leaked_terminal_responses(text) == "abcd"
|
||||
|
||||
def test_strips_visible_form_dsr(self):
|
||||
# When an upstream filter has already stripped the ESC byte and
|
||||
# left the caret-escape representation in place.
|
||||
text = "^[[53;1R"
|
||||
assert _strip_leaked_terminal_responses(text) == ""
|
||||
|
||||
def test_strips_visible_form_dsr_in_middle_of_text(self):
|
||||
text = "typed^[[53;1Rmore"
|
||||
assert _strip_leaked_terminal_responses(text) == "typedmore"
|
||||
|
||||
def test_does_not_strip_user_text_with_R(self):
|
||||
# Don't over-match; user might genuinely type text containing [N;NR patterns.
|
||||
# Our regex requires the leading ESC or caret-escape, so bare
|
||||
# "[53;1R" as user text is preserved.
|
||||
text = "see section [53;1R for details"
|
||||
assert _strip_leaked_terminal_responses(text) == text
|
||||
|
||||
def test_does_not_strip_sgr_sequences(self):
|
||||
# Sanity: don't wipe legitimate terminal control sequences that
|
||||
# aren't DSR responses.
|
||||
text = "\x1b[31mred\x1b[0m"
|
||||
assert _strip_leaked_terminal_responses(text) == text
|
||||
|
||||
def test_preserves_multiline_content(self):
|
||||
text = "line 1\n\x1b[53;1Rline 2"
|
||||
assert _strip_leaked_terminal_responses(text) == "line 1\nline 2"
|
||||
Loading…
Add table
Add a link
Reference in a new issue