xonecas.io
64875
Mc

mini-coder / commit aa81e51

refactor(cli): stabilize reasoning output after turn completion

xonecas2026-03-14 aa81e514b1411bc736852a21e74a4231141e6172 parent 72833c9

3 files changed

src/cli/stream-render-content.ts+6−45
@@ -1,15 +1,12 @@
1-import * as c from "yoctocolors";
21 import { renderLine } from "./markdown.ts";
32 import { G, write, writeln } from "./output.ts";
43 import type { Spinner } from "./spinner.ts";
54
65 export class StreamRenderContent {
76 private inText = false;
8- private inReasoning = false;
97 private rawBuffer = "";
108 private streamedChars = 0;
119 private inFence = false;
12- private reasoningBlankLineRun = 0;
1310 private accumulatedText = "";
1411 private accumulatedReasoning = "";
1512
@@ -24,13 +21,10 @@ export class StreamRenderContent {
2421 }
2522
2623 hasOpenContent(): boolean {
27- return this.inText || this.inReasoning;
24+ return this.inText;
2825 }
2926
3027 appendTextDelta(delta: string): void {
31- if (this.inReasoning) {
32- this.flushOpenContent();
33- }
3428 if (!this.inText) {
3529 this.spinner.stop();
3630 write(`${G.reply} `);
@@ -42,49 +36,22 @@ export class StreamRenderContent {
4236 this.streamPartialRemainder();
4337 }
4438
45- appendReasoningDelta(delta: string, showReasoning: boolean): void {
39+ appendReasoningDelta(delta: string): void {
4640 this.accumulatedReasoning += delta;
47- if (!showReasoning) return;
48-
49- if (!this.inReasoning) {
50- if (this.inText) {
51- this.flushOpenContent();
52- }
53- this.spinner.stop();
54- writeln(`${G.info} ${c.dim("reasoning")}`);
55- this.inReasoning = true;
56- this.inFence = false;
57- }
58- this.rawBuffer += delta;
59- this.flushCompleteLines();
60- this.streamPartialRemainder();
6141 }
6242
6343 flushOpenContent(): void {
64- if (!this.inText && !this.inReasoning) return;
44+ if (!this.inText) return;
6545 this.streamPartialRemainder();
6646 writeln();
6747 this.inText = false;
68- this.inReasoning = false;
6948 this.inFence = false;
70- this.reasoningBlankLineRun = 0;
7149 this.rawBuffer = "";
7250 this.streamedChars = 0;
7351 }
7452
75- private renderSingleLine(raw: string): string | null {
76- const source = this.inReasoning ? raw.replace(/[ \t]+$/g, "") : raw;
77- if (this.inReasoning && source.trim() === "") {
78- this.reasoningBlankLineRun += 1;
79- if (this.reasoningBlankLineRun > 1) return null;
80- } else if (this.inReasoning) {
81- this.reasoningBlankLineRun = 0;
82- }
83-
84- if (this.inReasoning) {
85- return ` ${c.dim(source)}`;
86- }
87- const rendered = renderLine(source, this.inFence);
53+ private renderSingleLine(raw: string): string {
54+ const rendered = renderLine(raw, this.inFence);
8855 this.inFence = rendered.inFence;
8956 return rendered.output;
9057 }
@@ -105,8 +72,7 @@ export class StreamRenderContent {
10572 }
10673 writeln();
10774 } else {
108- if (rendered !== null) writeln(rendered);
109- else writeln();
75+ writeln(rendered);
11076 }
11177
11278 this.rawBuffer = this.rawBuffer.slice(boundary + 1);
@@ -124,11 +90,6 @@ export class StreamRenderContent {
12490
12591 private writePartial(text: string): void {
12692 if (!text) return;
127- if (this.inReasoning) {
128- const prefix = this.streamedChars === 0 ? " " : "";
129- write(`${prefix}${c.dim(text)}`);
130- return;
131- }
13293 write(text);
13394 }
13495 }
src/cli/stream-render.test.ts+15−0
@@ -348,6 +348,21 @@ describe("renderTurn", () => {
348348 expect(result.reasoningText).toBe("thinking");
349349 });
350350
351+ test("renders reasoning after assistant text for a stable final layout", async () => {
352+ captureStdout();
353+
354+ await renderTurn(
355+ eventsFrom([
356+ { type: "reasoning-delta", delta: "step" },
357+ { type: "text-delta", delta: "answer" },
358+ done(),
359+ ]),
360+ new Spinner(),
361+ );
362+
363+ expect(simulateTerminal(stdout)).toBe("◆ answer\n· reasoning\n step\n");
364+ });
365+
351366 test("stops spinner defensively before each rendered line", async () => {
352367 captureStdout();
353368 const spinner = new Spinner();
src/cli/stream-render.ts+26−3
@@ -31,6 +31,27 @@ export async function renderTurn(
3131 let newMessages: CoreMessage[] = [];
3232 const startedToolCalls = new Set<string>();
3333
34+ let reasoningComputed = false;
35+ let reasoningText = "";
36+ const getReasoningText = (): string => {
37+ if (!reasoningComputed) {
38+ reasoningText = normalizeReasoningText(content.getReasoning());
39+ reasoningComputed = true;
40+ }
41+ return reasoningText;
42+ };
43+
44+ const renderReasoningBlock = (): boolean => {
45+ if (!showReasoning) return false;
46+ const text = getReasoningText();
47+ if (!text) return false;
48+ writeln(`${G.info} ${c.dim("reasoning")}`);
49+ for (const line of text.split("\n")) {
50+ writeln(` ${c.dim(line)}`);
51+ }
52+ return true;
53+ };
54+
3455 for await (const event of events) {
3556 switch (event.type) {
3657 case "text-delta": {
@@ -39,7 +60,8 @@ export async function renderTurn(
3960 }
4061 case "reasoning-delta": {
4162 const delta = normalizeReasoningDelta(event.delta);
42- content.appendReasoningDelta(delta, showReasoning);
63+ content.appendReasoningDelta(delta);
64+ reasoningComputed = false;
4365 break;
4466 }
4567
@@ -79,7 +101,8 @@ export async function renderTurn(
79101 const hadContent = content.hasOpenContent();
80102 content.flushOpenContent();
81103 spinner.stop();
82- if (!hadContent) writeln();
104+ const renderedReasoning = renderReasoningBlock();
105+ if (!hadContent && !renderedReasoning) writeln();
83106 inputTokens = event.inputTokens;
84107 outputTokens = event.outputTokens;
85108 contextTokens = event.contextTokens;
@@ -109,6 +132,6 @@ export async function renderTurn(
109132 outputTokens,
110133 contextTokens,
111134 newMessages,
112- reasoningText: normalizeReasoningText(content.getReasoning()),
135+ reasoningText: getReasoningText(),
113136 };
114137 }