Skip to content

fix(tui): strip ANSI from input buffer after gateway pipe break (#28419) - #29252

Closed
Jiahui-Gu wants to merge 1 commit into
NousResearch:mainfrom
Jiahui-Gu:fix/tui-ansi-flood-pipe-break-28419
Closed

fix(tui): strip ANSI from input buffer after gateway pipe break (#28419)#29252
Jiahui-Gu wants to merge 1 commit into
NousResearch:mainfrom
Jiahui-Gu:fix/tui-ansi-flood-pipe-break-28419

Conversation

@Jiahui-Gu

Copy link
Copy Markdown
Contributor

Summary

Fixes #28419. When the TUI gateway pipe breaks and the gateway auto-restarts, render bytes from stdout can leak into stdin. The composer's TextInput previously stripped only bracketed-paste markers (ESC[200~ / ESC[201~), so anything between them — including chunked / de-escaped CSI sequences like 35;104;62m — slipped through PRINTABLE and ended up in the input field, producing the characteristic ANSI flood reported in the issue.

Fix

ui-tui/src/components/textInput.tsx:

  • New exported stripAnsiSequences() helper that strips full CSI / OSC / SS3 / two-byte-ESC / bare-ESC / C0+C1 control bytes.
  • When residue still shows the orphan-CSI-flood signature (multiple \d+;\d+;\d+[A-Za-z] chains), an iterative cleanup pass removes leftover digits, ;, and stray CSI final letters. Ordinary user text (a; b; c, color: red;, 12;34) is preserved because the aggressive pass only fires when the orphan-flood signature is present.
  • Applied at three paste-like input sites: the bracketed-paste branch in useInput, flushPaste(), and pastePlainText().

Test plan

  • ui-tui/src/__tests__/textInputAnsiStrip.test.ts — 8 cases including the exact garbage shape from the bug report (102;71M5;104;62M...\"\"), bracketed-paste-wrapped ANSI leaks, unicode/punctuation preservation
  • vitest run shows no new regressions (2 pre-existing failures cursorDriftRegression, ink-resize also fail on main)
  • tsc --noEmit clean

🤖 Generated with Claude Code

…Research#28419)

When the TUI gateway's stdout pipe breaks mid-render and the gateway
auto-restarts, rendering ANSI bytes (color codes, cursor moves, OSC
title sets) can leak onto stdin and be delivered to the composer as
if the user had typed them. The bug report shows the input box
flooded with strings like "102;71M5;104;62M5;106;60M..." — the tail
end of de-escaped CSI parameter lists whose ESC prefix was consumed
earlier in the byte stream.

Add stripAnsiSequences() and apply it at every paste-like input path
in TextInput:
  - the bracketed-paste branch (after BRACKET_PASTE marker removal)
  - flushPaste()
  - pastePlainText() (OS clipboard / right-click paste)

The helper strips full CSI / OSC / SS3 / two-byte ESC sequences and,
when the residue still has the unmistakable shape of an orphan CSI
param flood (multiple "<digits>;<digits>;<digits><letter>" chains),
runs an iterative cleanup pass to remove the leftover digits, semicolons
and stray CSI final bytes. Ordinary user text like "a; b; c" or
"color: red;" is preserved because the aggressive pass only fires
when the orphan-flood signature is present.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/tui Terminal UI (ui-tui/ + tui_gateway/) labels May 20, 2026
@Jiahui-Gu

Copy link
Copy Markdown
Contributor Author

FYI for maintainer triage: noticed there's another open PR #28463 (fix(tui): recover degraded focus burst input) also targeting #28419, but with a different fix approach.

This PR specifically strips ANSI sequences from the input buffer (stripAnsiSequences() helper in textInput.tsx) to handle the chunked/de-escaped CSI sequences that leak through when the gateway pipe breaks and restarts. If #28463 already covers the input-buffer ANSI leak, happy to close. Different root cause angles may converge to the same fix — flagging so reviewers can pick whichever shape they prefer.

@teknium1

teknium1 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Closing as redundant against current main.

This is part of the TUI/CLI ANSI/SGR/escape-leak cluster, most of which is already solved on main:

Your work here overlapped this already-merged coverage. Thanks for the contribution and for helping surface the leak class — it's appreciated.

@teknium1 teknium1 closed this Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tui Terminal UI (ui-tui/ + tui_gateway/) P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TUI input box gets flooded with ANSI escape sequences after gateway pipe break

4 participants