fix(pi-tui): handle above-viewport changes without a destructive redraw
Related Issue
Resolve #2098
Problem
While a subagent runs, status rows above the visible window (the subagent header's spinner/elapsed timer ticking at 80ms, live progress lines) kept marking content above the viewport as changed. Every such frame took the "first changed line is above the viewport" branch and did a destructive full redraw (ESC[2J/ESC[3J), which cleared scrollback and snapped the terminal back to the bottom — making it impossible to scroll up and read history during subagent execution (macOS and Windows).
What changed
In packages/pi-tui's differential renderer, the above-viewport branch no longer falls back to a destructive full redraw:
- In-place edits above the window (
delta === 0, e.g. the subagent header tick): the diff is clamped to the visible window; when nothing visible changed, nothing is painted and only the frame caches are updated. - Length changes above the window (progress lines inserted, step rows collapsing): the viewport anchor follows the content by the delta — the screen already displays the shifted rows — and only the mismatched window rows are repainted in place. No scrolling, no
ESC[3J. - Destructive fallback kept for the cases that genuinely need it: content collapsing to at/above the window top (
/clear, compaction), out-of-range anchors, and frames involving kitty image lines.
This is deliberately narrower than the fork's earlier viewport/scrollback patches (reverted in 23daf0f3): it never scroll-commits and never repaints a rewound window, which is where the blank-screen / duplicated-scrollback edge cases came from. Accepted trade-offs, documented in code and packages/pi-tui/AGENTS.md: scrollback keeps stale bytes for changed above-window rows (a terminal cannot edit scrollback without clearing), and a shrink rewind may later re-commit a bounded span.
Tests: a new TUI above-viewport changes suite pins the behavior — in-place edits, mixed visible edits, a 20-frame subagent-style ticking simulation (asserts zero full redraws, no ESC[3J, byte-identical scrollback), insertion/shrink re-anchoring with commit-continuity checks, mixed shift+edit frames, and the collapse fallback. The pre-existing transient-component test now asserts the non-destructive behavior. Full pi-tui suite (743 tests) and the kimi-code vitest suite (2380 tests) pass.
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: d55235737b0c9ee67870b9cfbde9c17f0a4d802e
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/kimi-code | Patch |
| @moonshot-ai/pi-tui | 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