← Back to Pull requests

fix(tui): skip destructive redraw for in-place changes above the viewport #2205

Open
unknown opened this pull request with 459 lines diff across 5 files
View on GitHub
unknown unknown commented on Jul 26, 2026 09:40 UTC
Author

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

  • I have read the CONTRIBUTING document.

  • I have linked a related issue, or explained the problem above.

  • I have added tests that prove my feature works.

  • Ran gen-changesets skill, or this PR needs no changeset.

  • Ran gen-docs skill, or this PR needs no doc update.
  • Diff: 459 lines (+344 / -115) across 5 files Reviewer: HelloWorldU Review duration: 104360m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.53) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 459 lines diff across 5 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.367
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2205"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.5% (MEDIUM) · Higher = larger blast radius
    52.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29% · Safe to merge
    29%