xonecas.io
64875
Mc

mini-coder / commit 4058cf2

fix(cli): skip markdown highlighter when stdout is not a TTY

yoctomarkdown emits \x1b[2K\r (erase-line + CR) sequences for live
in-place rewrites. These are TTY-only and produce garbage when stdout is
piped to a file, another tool, or captured in CI.

Gate highlighter creation on terminal.isStdoutTTY (mirrors the existing
Spinner gate on isStderrTTY). When no highlighter, fall through to
write(text) so the raw content still streams out correctly.

Adds a non-TTY test asserting no \x1b[2K or \r in piped output.

xonecas2026-03-15 4058cf2ef64bc0b61d29faeef3a53606252e9927 parent 6dac72f

2 files changed

src/cli/stream-render-content.ts+4−1
@@ -1,6 +1,7 @@
11 import { createHighlighter, type Highlighter } from "yoctomarkdown";
22 import { G, write, writeln } from "./output.ts";
33 import type { Spinner } from "./spinner.ts";
4+import { terminal } from "./terminal-io.ts";
45
56 export class StreamRenderContent {
67 private inText = false;
@@ -33,7 +34,7 @@ export class StreamRenderContent {
3334 if (renderedVisibleOutput) writeln();
3435 write(`${G.reply} `);
3536 this.inText = true;
36- this.highlighter = createHighlighter();
37+ if (terminal.isStdoutTTY) this.highlighter = createHighlighter();
3738 }
3839 const isFirstLine = !this.accumulatedText.includes("\n");
3940 this.accumulatedText += text;
@@ -46,6 +47,8 @@ export class StreamRenderContent {
4647 }
4748 write(colored);
4849 }
50+ } else {
51+ write(text);
4952 }
5053 }
5154
src/cli/stream-render.test.ts+26−0
@@ -390,6 +390,32 @@ describe("renderTurn", () => {
390390 expect(plain).toContain("· skill deploy · local · Deploy app");
391391 });
392392
393+ test("non-TTY stdout: writes raw text without escape sequences", async () => {
394+ const originalIsTTY = process.stdout.isTTY;
395+ try {
396+ // biome-ignore lint/suspicious/noExplicitAny: test-only TTY override
397+ (process.stdout as any).isTTY = false;
398+ captureStdout();
399+
400+ await renderTurn(
401+ eventsFrom([
402+ { type: "text-delta", delta: "hello " },
403+ { type: "text-delta", delta: "world" },
404+ done(),
405+ ]),
406+ new Spinner(),
407+ );
408+ } finally {
409+ // biome-ignore lint/suspicious/noExplicitAny: test-only TTY override
410+ (process.stdout as any).isTTY = originalIsTTY;
411+ }
412+
413+ // No erase-line or carriage-return sequences in output
414+ expect(stdout.includes("\x1b[2K")).toBe(false);
415+ expect(stdout.includes("\r")).toBe(false);
416+ expect(strip(stdout)).toBe("◆ hello world\n");
417+ });
418+
393419 test("deduplicates repeated tool-call-start events for the same toolCallId", async () => {
394420 captureStdout();