fix(tui): skip destructive redraw for in-place changes above the viewport
Related Issue
Resolve #2039
Problem
While subagents run, the agents status header re-renders several times per second. Once /usage (or any appended content) pushes that ticking line above the visible viewport, every tick hits the firstChanged < prevViewportTop branch in doRender, which calls fullRender(true) — a destructive ESC[2J ESC[H ESC[3J clear plus a full transcript reprint. The result is sustained flicker (reported on Alacritty) and scroll-position yanks, several times per second, for as long as the status box keeps updating.
What changed
A deliberately narrow fast path in front of that branch, active only when the change is in place: same line count as the previous frame and no Kitty images (in either frame) in the above-viewport part of the changed range. Rows above the viewport are already committed to scrollback, so repainting them cannot change what the terminal shows:
- change entirely above the viewport → skip the paint, commit the line caches, reposition the hardware cursor (mirrors the existing no-change path).
- change spanning the viewport boundary → clamp the repaint range to the viewport top and continue on the regular differential path.
Trade-off to accept explicitly: scrollback keeps a stale frame of the skipped rows, and this is not limited to spinner glyphs — any same-line-count, image-free mutation above the viewport is skipped, so a scrolled-out status row can keep reading "running" in scrollback after the component moved on, until a full redraw repaints it. Nothing is lost relative to the previous behavior (which erased the entire scrollback several times per second), but the staleness is observable when scrolling up mid-task.
Termux is the one path where a height change deliberately avoids the destructive redraw, so a height increase there could re-expose skipped rows without a repaint. The follow-up commit tracks the lowest skipped row and invalidates the re-exposed cache entries so the differential path repaints them, with a regression test driving exactly that sequence (in-place tick above the viewport, then a grow).
Everything else — line-count changes, width/height changes, shrink, image lines — keeps the destructive full-redraw baseline restored in #1367. That revert rolled back #1315/#1353 because general diff-clamping produced blank screens, duplicated scrollback, and lost rows; this path cannot reproduce those artifact classes because it never repaints scrolled-out rows at all and never runs when the layout shifts.
Tests: a new "TUI above-viewport in-place changes" group in test/tui-render.test.ts (node:test, so the package test gate and the test-pi-tui CI job exercise it — a future re-vendor that clobbered the fix would fail acceptance) drives a VirtualTerminal taller-than-viewport transcript and asserts: repeated above-viewport ticks emit no ESC[2J/ESC[3J and don't increment fullRedraws while the viewport stays byte-identical; a boundary-spanning change repaints exactly the visible rows; an above-viewport line-count change still triggers the full redraw. The first two fail on main; the third pins the preserved baseline. The full suite passes (739 tests), and the behavior is recorded as divergence 6 in the vendored-fork ledger (packages/pi-tui/AGENTS.md).
Checklist
- [x] I have read the CONTRIBUTING document.
- [x] I have linked a related issue, or explained the problem above.
- [x] I have added tests that prove my feature works.
- [x] Ran
gen-changesetsskill, or this PR needs no changeset. - [x] Ran
gen-docsskill, or this PR needs no doc update.
🦋 Changeset detected
Latest commit: 2a6000a20775db600c50181e13532cc0c154674b
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
| Name | Type |
|---|---|
| @moonshot-ai/pi-tui | Patch |
| @moonshot-ai/kimi-code | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
@codex review — the Termux re-exposure case is addressed in 2a6000a2: the lowest skipped row is tracked and re-exposed cache entries are invalidated so the differential path repaints them, with a regression test driving the exact tick-then-grow sequence. The PR description now also states the stale-scrollback trade-off explicitly.
Codex Review: Didn't find any major issues. Hooray!
Reviewed commit: 2a6000a207
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
For this specific case, I think skipping redraw is the right direction, given the regressions from differential redraw and the limitations of destructive redraw with terminal scrollback. @liruifengv, do you think temporarily stale scrollback content is an acceptable trade-off for this narrow fix? Also, does the team have plans to revisit the rendering architecture, perhaps toward a more application-owned viewport model?
I just noticed that upstream pi has landed an experimental application-owned alternate-screen renderer (--alt, https://github.com/earendil-works/pi/issues/7304). Does the team plan to adopt or port this implementation into pi-tui?