Skip to content

The agent that grows with you

fix(cli): redraw on terminal focus regain + docs for scrollback rebuild config

Teknium3bd98ec4bfd3
diff --git a/cli.py b/cli.py
index a8b6572ec40..5ccf42d9393 100644
--- a/cli.py
+++ b/cli.py
@@ -5081,6 +5081,32 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin):
except Exception:
pass
+ def _schedule_focus_regain_redraw(self, min_interval: float = 1.0) -> None:
+ """Repaint after a terminal focus-in report (``CSI I``), rate-limited.
+
+ Terminals with focus tracking active (Ghostty, iTerm2, xterm builds,
+ multiplexers that toggle DECSET 1004 upstream) emit ``\\x1b[I`` when
+ the Hermes tab/window becomes visible again. Emulators can coalesce
+ or drop hidden-tab output and repaint the surface while we're
+ invisible, so on regain prompt_toolkit's incremental diff stacks on
+ stale content — a second copy of the composer/prompt chrome next to
+ the ghost of the old one (#60920 focus-regain variant, #25337).
+
+ The stock handling maps ``CSI I``/``CSI O`` to ``Keys.Ignore`` so the
+ bytes never pollute the input buffer; this hook additionally routes
+ focus-in through the same recovery as Ctrl+L / ``/redraw``. It is
+ self-gating: terminals that never enable focus tracking never emit
+ the sequence, so nothing changes for them. Rate-limited so a burst of
+ focus reports (rapid Alt+Tab, mux pane hops) repaints at most once
+ per ``min_interval`` seconds.
+ """
+ now = time.monotonic()
+ last = getattr(self, "_last_focus_regain_redraw", 0.0)
+ if now - last < min_interval:
+ return
+ self._last_focus_regain_redraw = now
+ self._force_full_redraw()
+
@staticmethod
def _redraw_rebuilds_scrollback() -> bool:
"""Return whether CLI redraw/resize recovery should clear scrollback.
@@ -16100,7 +16126,20 @@ class HermesCLI(CLIAgentSetupMixin, CLICommandsMixin, CLIBillingMixin):
VT100 parser level. Without this no-op binding the default
self-insert path would still fire and the bytes would land in
the buffer.
+
+ Focus-in (CSI I) additionally schedules a rate-limited full
+ repaint: while the tab/window was hidden the emulator may have
+ coalesced output or repainted the surface, so prompt_toolkit's
+ incremental diff would stack a fresh copy of the prompt chrome
+ on top of the stale one (#60920 focus-regain variant, #25337).
"""
+ try:
+ for press in getattr(event, "key_sequence", None) or ():
+ if getattr(press, "data", None) == "\x1b[I":
+ self._schedule_focus_regain_redraw()
+ break
+ except Exception:
+ pass
return None
def handle_enter(event):
diff --git a/tests/cli/test_cli_force_redraw.py b/tests/cli/test_cli_force_redraw.py
index cfd4fd95323..81d1312f5fa 100644
--- a/tests/cli/test_cli_force_redraw.py
+++ b/tests/cli/test_cli_force_redraw.py
@@ -389,3 +389,40 @@ class TestFirstSigwinchBaseline:
bare_cli._install_resize_recovery(app) # must not raise
assert getattr(bare_cli, "_last_resize_width", None) is None
+
+
+class TestFocusRegainRedraw:
+ """Focus-in (CSI I) routes through the same recovery as Ctrl+L, rate-limited.
+
+ While the tab/window is hidden the emulator may coalesce output or repaint
+ the surface; on regain prompt_toolkit's incremental diff stacks a fresh
+ copy of the prompt chrome on top of the stale one (#60920 focus-regain
+ variant, #25337).
+ """
+
+ def test_focus_regain_triggers_full_redraw(self, bare_cli):
+ calls = []
+ bare_cli._force_full_redraw = lambda: calls.append("redraw")
+
+ bare_cli._schedule_focus_regain_redraw()
+
+ assert calls == ["redraw"]
+
+ def test_focus_regain_redraw_is_rate_limited(self, bare_cli):
+ calls = []
+ bare_cli._force_full_redraw = lambda: calls.append("redraw")
+
+ bare_cli._schedule_focus_regain_redraw(min_interval=60.0)
+ bare_cli._schedule_focus_regain_redraw(min_interval=60.0)
+ bare_cli._schedule_focus_regain_redraw(min_interval=60.0)
+
+ assert calls == ["redraw"]
+
+ def test_focus_regain_redraw_fires_again_after_interval(self, bare_cli):
+ calls = []
+ bare_cli._force_full_redraw = lambda: calls.append("redraw")
+
+ bare_cli._schedule_focus_regain_redraw(min_interval=0.0)
+ bare_cli._schedule_focus_regain_redraw(min_interval=0.0)
+
+ assert calls == ["redraw", "redraw"]
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index 423dfc7b739..97e53ab083a 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -1749,6 +1749,7 @@ display:
fields: ["model", "context_pct", "cwd"]
file_mutation_verifier: true # Append an advisory footer when write_file/patch calls failed this turn
credits_notices: true # Nous credits status-bar notices (usage bands, grant-spent, depleted). false = silence them; /usage still works
+ cli_rebuild_scrollback_on_redraw: false # Classic CLI: also wipe terminal scrollback (CSI 3J) on /redraw / Ctrl+L / width-change resize recovery. Enable when a terminal/tmux stack stamps stale prompt chrome into scrollback on maximize/restore.
language: en # UI language for static messages (approval prompts, some gateway replies). en | zh | zh-hant | ja | de | es | fr | tr | uk | af | ko | it | ga | pt | ru | hu
```