xonecas.io
64875
Mc

mini-coder / commit 9e717a5

feat(cli): normalize and structure reasoning output

xonecas2026-03-13 9e717a5dd14e394c484870441e7cd3b78c827cfe parent 6becf56

5 files changed

TODO.md+1−0
@@ -5,6 +5,7 @@
55 - Tool hooks output prints inline with spinner
66 - When the user uses /new reset the terminal and clear the history, print the banner again
77 - Review our markdown renderer. Let's make it render the raw markdown, but syntax highlited
8+- add a spinner state showing the user when the undo snapshotting is happening
89
910 ---
1011
src/cli/headless-reporter.ts+6−2
@@ -6,6 +6,10 @@ import type {
66 } from "../agent/reporter.ts";
77 import type { CoreMessage } from "../llm-api/turn.ts";
88 import type { TurnEvent } from "../llm-api/types.ts";
9+import {
10+ normalizeReasoningDelta,
11+ normalizeReasoningText,
12+} from "./reasoning.ts";
913
1014 /**
1115 * A no-op reporter for headless (subprocess) mode.
@@ -36,7 +40,7 @@ export class HeadlessReporter implements AgentReporter {
3640 accumulatedText += event.delta;
3741 break;
3842 case "reasoning-delta":
39- reasoningText += event.delta;
43+ reasoningText += normalizeReasoningDelta(event.delta);
4044 break;
4145 case "context-pruned":
4246 break;
@@ -66,7 +70,7 @@ export class HeadlessReporter implements AgentReporter {
6670 outputTokens,
6771 contextTokens,
6872 newMessages,
69- reasoningText,
73+ reasoningText: normalizeReasoningText(reasoningText),
7074 };
7175 }
7276
src/cli/reasoning.ts+32−0
@@ -0,0 +1,32 @@
1+export function normalizeReasoningDelta(delta: string): string {
2+ return delta.replace(/\r\n?/g, "\n");
3+}
4+
5+export function normalizeReasoningText(text: string): string {
6+ const normalized = normalizeReasoningDelta(text);
7+ const lines = normalized
8+ .split("\n")
9+ .map((line) => line.replace(/[ \t]+$/g, ""));
10+
11+ let start = 0;
12+ while (start < lines.length && lines[start]?.trim() === "") start++;
13+
14+ let end = lines.length - 1;
15+ while (end >= start && lines[end]?.trim() === "") end--;
16+
17+ if (start > end) return "";
18+
19+ const compact: string[] = [];
20+ let blankRun = 0;
21+ for (const line of lines.slice(start, end + 1)) {
22+ if (line.trim() === "") {
23+ blankRun += 1;
24+ if (blankRun <= 1) compact.push("");
25+ continue;
26+ }
27+ blankRun = 0;
28+ compact.push(line);
29+ }
30+
31+ return compact.join("\n");
32+}
src/cli/stream-render.test.ts+20−2
@@ -120,7 +120,25 @@ describe("renderTurn", () => {
120120 expect(result.reasoningText).toBe("step 1\nstep 2");
121121 });
122122
123- test("renders reasoning output by default and accumulates it", async () => {
123+ test("normalizes reasoning whitespace safely", async () => {
124+ captureStdout();
125+
126+ const result = await renderTurn(
127+ eventsFrom([
128+ {
129+ type: "reasoning-delta",
130+ delta: "\r\n\r\n hello \r\n\r\n\r\nworld\t\t\r\n\r\n",
131+ },
132+ done(),
133+ ]),
134+ new Spinner(),
135+ { showReasoning: false },
136+ );
137+
138+ expect(result.reasoningText).toBe(" hello\n\nworld");
139+ });
140+
141+ test("renders reasoning in a structured block and accumulates it", async () => {
124142 captureStdout();
125143
126144 const result = await renderTurn(
@@ -128,7 +146,7 @@ describe("renderTurn", () => {
128146 new Spinner(),
129147 );
130148
131- expect(strip(stdout)).toBe("thinking\n");
149+ expect(strip(stdout)).toBe("· reasoning\n│ thinking\n");
132150 expect(hasAnsi(stdout, "[2m")).toBe(true);
133151 expect(result.reasoningText).toBe("thinking");
134152 });
src/cli/stream-render.ts+24−5
@@ -6,6 +6,10 @@ import { logError } from "./error-log.ts";
66 import { parseAppError } from "./error-parse.ts";
77 import { renderLine } from "./markdown.ts";
88 import { G, write, writeln } from "./output.ts";
9+import {
10+ normalizeReasoningDelta,
11+ normalizeReasoningText,
12+} from "./reasoning.ts";
913 import type { Spinner } from "./spinner.ts";
1014 import { renderToolCall, renderToolResult } from "./tool-render.ts";
1115
@@ -28,6 +32,7 @@ export async function renderTurn(
2832 let accumulatedReasoning = "";
2933
3034 let inFence = false;
35+ let reasoningBlankLineRun = 0;
3136
3237 let inputTokens = 0;
3338 let outputTokens = 0;
@@ -38,9 +43,20 @@ export async function renderTurn(
3843 // Defensive stop: some providers stream tiny chunks and spinner ticks can
3944 // interleave with stdout writes unless we stop right before each render.
4045 spinner.stop();
41- const rendered = renderLine(raw, inFence);
46+
47+ const source = inReasoning ? raw.replace(/[ \t]+$/g, "") : raw;
48+ if (inReasoning && source.trim() === "") {
49+ reasoningBlankLineRun += 1;
50+ if (reasoningBlankLineRun > 1) return;
51+ } else if (inReasoning) {
52+ reasoningBlankLineRun = 0;
53+ }
54+
55+ const rendered = renderLine(source, inFence);
4256 inFence = rendered.inFence;
43- const finalOutput = inReasoning ? c.dim(rendered.output) : rendered.output;
57+ const finalOutput = inReasoning
58+ ? `${c.dim("│ ")}${rendered.output}`
59+ : rendered.output;
4460 write(finalOutput);
4561 if (endWithNewline) {
4662 write("\n");
@@ -53,6 +69,7 @@ export async function renderTurn(
5369 writeln();
5470 inText = false;
5571 inReasoning = false;
72+ reasoningBlankLineRun = 0;
5673 }
5774 }
5875
@@ -91,7 +108,8 @@ export async function renderTurn(
91108 break;
92109 }
93110 case "reasoning-delta": {
94- accumulatedReasoning += event.delta;
111+ const delta = normalizeReasoningDelta(event.delta);
112+ accumulatedReasoning += delta;
95113 if (!showReasoning) {
96114 break;
97115 }
@@ -100,9 +118,10 @@ export async function renderTurn(
100118 flushAnyText();
101119 }
102120 spinner.stop();
121+ writeln(`${G.info} ${c.dim("reasoning")}`);
103122 inReasoning = true;
104123 }
105- rawBuffer += event.delta;
124+ rawBuffer += delta;
106125 flushCompleteLines();
107126 break;
108127 }
@@ -174,6 +193,6 @@ export async function renderTurn(
174193 outputTokens,
175194 contextTokens,
176195 newMessages,
177- reasoningText: accumulatedReasoning,
196+ reasoningText: normalizeReasoningText(accumulatedReasoning),
178197 };
179198 }