Skip to content

fix(tui): sanitize foreground Bash tool output - #2919

Open
nhatduy227 wants to merge 1 commit into
MoonshotAI:mainfrom
nhatduy227:fix/sanitize-foreground-bash-output
Open

fix(tui): sanitize foreground Bash tool output#2919
nhatduy227 wants to merge 1 commit into
MoonshotAI:mainfrom
nhatduy227:fix/sanitize-foreground-bash-output

Conversation

@nhatduy227

@nhatduy227 nhatduy227 commented Aug 14, 2026

Copy link
Copy Markdown

Related Issue

Fixes #2915

Problem

Foreground Bash tool stdout/stderr reaches pi-tui unsanitized, both while streaming and in the final result. A captured ESC[?1049h switches the host terminal to the alternate screen; inside tmux this makes native scrollback unavailable, and the pane stays in that state after Kimi Code exits because no matching ESC[?1049l is emitted.

sanitizeShellOutput already guards ! shell mode and the background task views (#2863), but not the tool-call card.

What changed

Sanitize result.output in ShellExecutionComponent before styling. This single site covers both the accumulated live stdout/stderr and the final Bash result, since buildLiveOutputBlock renders live output through the same component. Sanitizing the complete accumulated buffer rather than each chunk means an escape sequence split across appendLiveOutput calls cannot survive.

Scoped to ShellExecutionComponent rather than TruncatedOutputComponent, which is the default result renderer and would change generic and MCP tool rendering too.

Includes a patch changeset for @moonshot-ai/kimi-code.

Verification

CI has not been able to run on this fork PR yet, so every ci.yml job was reproduced locally on cec058a (Node 25.8.0, pnpm 10.33.0):

ci.yml job Command Result
test pnpm run test 1108 files passed, 18473 tests passed, 0 failed
test-pi-tui pnpm --filter @moonshot-ai/pi-tui test 939/939 passed
lint pnpm run lint / pnpm run sherif 0 errors / no issues
typecheck pnpm run typecheck clean
build pnpm run build + pnpm -C apps/kimi-code run smoke build OK, bundle smoke passed

Nix Build was not reproducible locally (nix not installed), but this PR touches no packaging or flake inputs.

Also measured the sanitizer at the 50k MAX_LIVE_OUTPUT_CHARS cap: ~0.075 ms per call on escape-heavy input, so the per-render cost is negligible.

Note on #2918

#2918 (interactive PTY ! commands) raises sanitize-vs-interpret for transcript rendering. That concerns ! shell mode; the agent-invoked Bash tool is non-interactive by design, so sanitizing its captured output stays correct either way.

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.

@changeset-bot

changeset-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cec058a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@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

Captured Bash stdout/stderr reached pi-tui unsanitized in the tool-call
card, so sequences such as ESC[?1049h switched the host terminal to the
alternate screen and left tmux scrollback unavailable after exit.

Sanitize the accumulated buffer in ShellExecutionComponent, which covers
both streaming live output and the final result.
@nhatduy227
nhatduy227 force-pushed the fix/sanitize-foreground-bash-output branch from 910d9ef to cec058a Compare August 14, 2026 08:17
@nhatduy227

nhatduy227 commented Aug 14, 2026

Copy link
Copy Markdown
Author

@liruifengv — tagging you since this continues #2863 (which you merged). The foreground Bash tool card still passes captured output straight to pi-tui, so ESC[?1049h from command output flips the host terminal to the alternate screen and takes tmux scrollback with it.

Diff is one call site plus a changeset. Happy to add regression tests for the split-chunk and final-result paths if you'd like them in this PR.

@nhatduy227

nhatduy227 commented Aug 19, 2026

Copy link
Copy Markdown
Author

Friendly bump. Rechecked against current main (50 commits later): still conflict-free, and shell-execution.ts / shell-output.ts are untouched, so #2915 is still open and this still applies.

Regression tests for the split-chunk and final-result paths are ready to push if you want them in this PR — just say so. Happy to redirect to someone else if the TUI area has a different owner now.

cc: @liruifengv

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.

Foreground Bash tool output renders raw terminal escape sequences (breaks tmux scrollback)

1 participant