xonecas.io
64875
Mc

mini-coder / commit 89e140b

fix(tui): drop control bytes and non-SGR escapes before rendering

A tool's output reached the terminal unfiltered, so a `\r`, an `ESC[2J` or
an `ESC[5A` inside a result moved the cursor and rewrote or erased
committed scrollback rows.

- sanitize() keeps SGR — the TUI's own styling and any color a tool emits
  — and drops every other escape sequence and control byte.
- it runs before the text is measured, so a row's width is its visible
  width, and at both display boundaries: renderRows (content lines,
  committed and in flight) and push (composed rows: call lines and
  terminal states).

ux-walk-20260924 finding 1.

xonecas2026-09-25 89e140b2a9b996f495160fa30710ce6fd1999ace parent 7841cb1

2 files changed

src/tui/term.ts+13−0
@@ -37,6 +37,19 @@ export function displayWidth(text: string): number {
3737 /** An SGR sequence: zero cells wide, so it never affects a row break. */
3838 const SGR = /^\x1b\[[0-9;]*m/;
3939
40+/**
41+ * Everything a tool, a file or a model can emit that the terminal would act on
42+ * but the TUI did not write itself: control bytes and every escape sequence but
43+ * SGR. Dropped before the text is measured, so a row's width is its visible
44+ * width and a result can only add rows — never move the cursor, erase the
45+ * screen, or break the row model. Tabs survive to `expandTabs`, which owns them.
46+ */
47+const UNSAFE = /(\x1b\[[0-9;]*m)|\x1b(?:\[[0-9;?]*[ -/]*[@-~]|.)|[\x00-\x08\x0a-\x1f\x7f-\x9f]/g;
48+
49+export function sanitize(text: string): string {
50+ return text.replace(UNSAFE, (_match, sgr: string | undefined) => sgr ?? "");
51+}
52+
4053 export function expandTabs(text: string, size = 4): string {
4154 if (!text.includes("\t")) return text;
4255 let column = 0;
src/tui/tui.ts+3−2
@@ -1,7 +1,7 @@
11 import process from "node:process";
22 import type { AssistantMessage, JsonObject, Message, UserMessage } from "@earendil-works/pi-ai";
33 import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "../agent.ts";
4-import { Terminal, expandTabs, wrapLine, type Key } from "./term.ts";
4+import { Terminal, expandTabs, sanitize, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
66 import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
77 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
@@ -70,7 +70,7 @@ function resultLines(name: string, text: string, isError: boolean): BodyLine[] {
7070 function renderRows(lines: BodyLine[], width: number): string[] {
7171 return lines.flatMap((line) => {
7272 const style = line.style;
73- const wrapped = wrapLine(expandTabs(line.text), width);
73+ const wrapped = wrapLine(expandTabs(sanitize(line.text)), width);
7474 return style === undefined ? wrapped : wrapped.map((row) => (row === "" ? row : style(row)));
7575 });
7676 }
@@ -461,6 +461,7 @@ class Tui {
461461 * and two blank lines never appear in a row.
462462 */
463463 private push(line: string): void {
464+ line = sanitize(line);
464465 const blank = line === "" || (this.separator && this.wrote);
465466 this.separator = false;
466467 if (blank && this.wrote && !this.lastBlank) {