xonecas.io
64875
Mc

mini-coder / commit e1e7f48

fix(cli): stream reasoning output immediately

xonecas2026-03-15 e1e7f484d3ed98b0c154f84eae79f06d379cf7b9 parent 95704e3

3 files changed

src/cli/live-reasoning.test.ts+49−0
@@ -0,0 +1,49 @@
1+import { afterEach, describe, expect, test } from "bun:test";
2+import { LiveReasoningBlock } from "./live-reasoning.ts";
3+import { terminal } from "./terminal-io.ts";
4+
5+let stdout = "";
6+const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
7+
8+afterEach(() => {
9+ stdout = "";
10+ terminal.stdoutWrite = originalStdoutWrite;
11+});
12+
13+function captureStdout(): void {
14+ terminal.stdoutWrite = (text: string) => {
15+ stdout += text;
16+ };
17+}
18+
19+function stripAnsi(text: string): string {
20+ const esc = String.fromCharCode(0x1b);
21+ return text.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
22+}
23+
24+describe("LiveReasoningBlock", () => {
25+ test("streams partial reasoning immediately instead of waiting for finish", () => {
26+ captureStdout();
27+ const block = new LiveReasoningBlock();
28+
29+ block.append("think");
30+ expect(stripAnsi(stdout)).toBe("· reasoning\n think");
31+
32+ block.append("ing");
33+ expect(stripAnsi(stdout)).toBe("· reasoning\n thinking");
34+
35+ block.finish();
36+ expect(stripAnsi(stdout)).toBe("· reasoning\n thinking\n");
37+ });
38+
39+ test("preserves blank reasoning lines and italic styling", () => {
40+ captureStdout();
41+ const block = new LiveReasoningBlock();
42+
43+ block.append("line 1\n\nline 3");
44+ block.finish();
45+
46+ expect(stripAnsi(stdout)).toBe("· reasoning\n line 1\n \n line 3\n");
47+ expect(stdout.includes("\x1b[3m")).toBe(true);
48+ });
49+});
src/cli/live-reasoning.ts+25−16
@@ -1,24 +1,32 @@
11 import * as c from "yoctocolors";
2-import { G, writeln } from "./output.ts";
2+import { G, write, writeln } from "./output.ts";
3+
4+const ITALIC_ON = "\x1b[3m";
5+const ITALIC_OFF = "\x1b[23m";
6+
7+function styleReasoningText(text: string): string {
8+ return `${ITALIC_ON}${c.dim(text)}${ITALIC_OFF}`;
9+}
310
411 export class LiveReasoningBlock {
512 private blockOpen = false;
6- private pending = "";
13+ private lineOpen = false;
714
815 append(delta: string): void {
916 if (!delta) return;
1017 this.openBlock();
11- this.pending += delta;
12- this.flushCompleteLines();
18+ const lines = delta.split("\n");
19+ for (const [index, line] of lines.entries()) {
20+ if (line) this.writeText(line);
21+ if (index < lines.length - 1) this.endLine();
22+ }
1323 }
1424
1525 finish(): void {
1626 if (!this.blockOpen) return;
17- if (this.pending.length > 0) {
18- this.writeLine(this.pending);
19- this.pending = "";
20- }
27+ if (this.lineOpen) writeln();
2128 this.blockOpen = false;
29+ this.lineOpen = false;
2230 }
2331
2432 private openBlock(): void {
@@ -27,16 +35,17 @@ export class LiveReasoningBlock {
2735 this.blockOpen = true;
2836 }
2937
30- private flushCompleteLines(): void {
31- let boundary = this.pending.indexOf("\n");
32- while (boundary !== -1) {
33- this.writeLine(this.pending.slice(0, boundary));
34- this.pending = this.pending.slice(boundary + 1);
35- boundary = this.pending.indexOf("\n");
38+ private writeText(text: string): void {
39+ if (!this.lineOpen) {
40+ write(" ");
41+ this.lineOpen = true;
3642 }
43+ write(styleReasoningText(text));
3744 }
3845
39- private writeLine(line: string): void {
40- writeln(` ${c.dim(line)}`);
46+ private endLine(): void {
47+ if (!this.lineOpen) write(" ");
48+ writeln();
49+ this.lineOpen = false;
4150 }
4251 }
src/cli/stream-render.test.ts+1−0
@@ -318,6 +318,7 @@ describe("renderTurn", () => {
318318
319319 expect(simulateTerminal(stdout)).toBe("· reasoning\n thinking\n");
320320 expect(hasAnsi(stdout, "[2m")).toBe(true);
321+ expect(hasAnsi(stdout, "[3m")).toBe(true);
321322 expect(result.reasoningText).toBe("thinking");
322323 });