Skip to content

fix(tui): stop backgrounded shell cards from forcing a full-screen redraw every second - #3751

Open
kimi-agent-bot wants to merge 3 commits into
MoonshotAI:mainfrom
kimi-agent-bot:fix/tui-viewport-full-redraw
Open

kimi-agent-bot wants to merge 3 commits into
MoonshotAI:mainfrom
kimi-agent-bot:fix/tui-viewport-full-redraw

Conversation

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator

Problem

In a long TUI session, any card that updates once per second (a running background task card, or a zombie card left behind after a ! command is auto-backgrounded at 120s) forces a clear-screen full redraw on every tick once it scrolls above the visible viewport: ESC[2J ESC[H ESC[3J followed by re-printing the entire transcript from line 0 (~51KB per redraw, ~1Hz, ~3MB/min). The terminal visibly scrolls through history every second, flickers constantly, and ESC[3J destroys scrollback every second.

Root cause

  1. ShellRunComponent runs a 1s setInterval (apps/kimi-code/src/tui/components/messages/shell-run.ts) → requestRender → TuiMainScreen.doRender line-diffs to firstChanged = the card's row → firstChanged < prevViewportTop → fullRender(true) which emits the clear-screen + clear-scrollback sequence plus the full text. This path had no rate limit and no "don't redraw invisible lines" protection.
  2. Zombie card driver: finishShellOutput in kimi-tui.ts returned early when backgrounded === true, so finish() was never called on the shell-run component after the engine auto-backgrounds a foreground shell command at the 120s timeout (shellCommandService.ts resolves {backgrounded: true} without going through the manual ctrl+b detach path). The card's 1s timer therefore ran forever.

Fixes

  • Zombie card lifecycle: the auto-backgrounded branch of finishShellOutput now mirrors the manual detach path — finishBackgrounded() stops the timer and renders the backgrounded view.
  • No ESC[3J on automatic redraws: fullRender(clear, clearScrollback) splits the two semantics; scrollback clearing is kept only for explicit user actions (terminal resize). All automatic fallback full redraws no longer destroy scrollback.
  • Rate limit: automatic clear-full-redraws are throttled to at most one per 2s, merging requests inside the window into a single trailing redraw. Explicit paths (first render, resize, forced render) are unaffected.
  • Above-viewport changes (conservative): when the line count is unchanged and the changed range contains no kitty images (line-index↔screen-row mapping is strictly preserved): a change entirely above the viewport writes no bytes and simply adopts the new frame; a change spanning the viewport boundary clamps firstChanged to the viewport top and takes the normal diff path so visible rows update immediately. Line-count changes or image changes still take the (now rate-limited) full redraw. Trade-off, documented in code and pi-tui/AGENTS.md: a card entirely above the viewport keeps its stale text in scrollback until the next explicit full redraw.

Regression tests

  • packages/pi-tui/test/tui-render.test.ts: new describe block covering confined-above-viewport skip, spanning-boundary clamp, rate limiting, and absence of ESC[3J on automatic redraws / presence on resize. Verified failing before the fix, passing after.
  • apps/kimi-code/test/tui/kimi-tui-message-flow.test.ts: auto-backgrounded shell command no longer leaves a ticking zombie card.

pi-tui 978/978 pass; kimi-code test/tui 2680/2681 pass (the one failure, commands/registry.test.ts ~/ completion, also fails on a clean base in this container — empty $HOME, unrelated). typecheck (pi-tui, kimi-code) and pnpm lint green.

Notes for reviewers

This intentionally takes a conservative approach in the core render path: the above-viewport optimization only applies when the line mapping cannot shift (equal line count, no images), and everything else degrades to a rate-limited full redraw rather than being skipped.

@changeset-bot

changeset-bot Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0881616

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@0881616
npx https://pkg.pr.new/@moonshot-ai/kimi-code@0881616

commit: 0881616

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6c81c0418

ℹ️ 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".

Comment thread packages/pi-tui/src/tui-main-screen.ts Resolved
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 45beb19463

ℹ️ 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".

Comment thread packages/pi-tui/src/tui-main-screen.ts Resolved
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: a3f19bfd36

ℹ️ 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".

…w every frame

When the first changed line sat above the previous viewport, the
differential renderer fell back to a clear-full-redraw (ESC[2J/H/3J plus
the whole transcript), so any card ticking above the viewport reprinted
and wiped scrollback on every tick.

- adopt in-place edits confined above the viewport without redrawing and
  clamp spanning in-place edits to the visible range instead of
  full-redrawing the transcript
- split scrollback clearing (ESC[3J) out of the automatic fallback
  redraw; it now only runs for explicit actions like terminal resize
- rate-limit automatic clear-full-redraws to one per 2s on a monotonic
  performance.now() clock (a wall-clock rollback must not stretch the
  window), merging requests inside the window into a single trailing
  redraw
- let input-driven frames bypass the rate limit: deferring them leaves
  previousLines stale and swallows typed text until the trailing timer
  fires; an interactive frame renders immediately, cancels the pending
  trailing redraw, and leaves the rate-limit clock untouched

Registers the divergence as an intent card in packages/pi-tui/UPSTREAM.md.
…nds a command

A `!` command auto-backgrounded by the engine's foreground timeout left
its card's 1s tick timer running forever: finishShellOutput returned
early on the backgrounded resolution, so the card never finalized. In
long sessions every tick landed above the viewport and forced a
clear-full-redraw of the transcript each second.

Mirror the manual ctrl+b detach path: finish the card as backgrounded,
set the transcript entry to 'Moved to background.', and drop the stream
so the tick stops. The engine's notification turn owns the model
notification and the streaming phase from there.
@kimi-agent-bot
kimi-agent-bot force-pushed the fix/tui-viewport-full-redraw branch from a3f19bf to 1afad5b Compare September 14, 2026 20:03
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1afad5b883

ℹ️ 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".

Comment thread packages/pi-tui/src/tui-main-screen.ts Resolved
…option

The guardrails landed unconditionally on TuiMainScreen, changing the
published renderer's default behavior for every consumer: automatic
fallback redraws could be deferred by up to 2s and no longer cleared the
scrollback, and above-viewport edits were adopted without redrawing,
even when the host never asked for the Kimi-specific mitigation. The
scoped fork rules (packages/pi-tui/AGENTS.md) require local library
patches to be additive and default-off with defaults matching upstream.

Add TuiMainScreenOptions.guardedAutomaticFullRedraws (default false).
Off matches upstream exactly: every fallback is an immediate
clear-full-redraw including the scrollback (ESC[2J/H/3J), no rate limit,
no above-viewport adoption. On enables the full bundle: above-viewport
in-place edit adoption/clamping, scrollback-preserving automatic
redraws, the 2s monotonic rate limit with a merged trailing redraw, and
the input-driven bypass. apps/kimi-code opts in for its main screen; the
single-shot exit-path renderer keeps the default (first paint never
reaches the fallback). Tests assert both the default upstream behavior
and the opted-in guardrails.
@kimi-agent-bot

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 0881616115

ℹ️ 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".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant