xonecas.io
64875
Mc

mini-coder / commit e414e12

perf: stream partial lines immediately for responsive output

Previously the renderer only wrote complete (newline-terminated) lines,
causing visible lag — especially for long paragraphs or single-sentence
replies where no output appeared until a newline arrived.

Each text-delta and reasoning-delta now writes new raw characters to the
terminal immediately via streamPartialLine(). When the line is eventually
complete, \r\x1b[2K overwrites the raw preview with the properly
markdown-styled version. Line prefixes (◆ for replies, │ for reasoning)
are handled correctly across both the preview and the overwrite path.

xonecas2026-03-14 e414e12c6b450c981c3544bc1b8ab890b288641c parent f3346d1

3 files changed

TODO.md+1−3
@@ -1,10 +1,8 @@
11 # TODO
22
3-- Most models hang for long periods during turns. Sometines needing the user to interrupt to continue
4-- Our output is not responsive. The user experience is poor right now, with long period of waiting for output. I've compared the same priovider and model with pi and opencode and their output is responsive and fast. This is critical to fix, it's part of our core idea and philosophy
3+- Most models hang for long periods during turns. Sometines needing the user to interrupt to continue. This happened in recent sessions in the db. This is not a provider issues, both opencode and pi using the same provider and model don't have hangups.
54
65 - Tool hooks output prints inline with spinner when they should output in a new line and indented like the tool calls output.
7-- Review our markdown renderer. Let's make it render the raw markdown, but syntax highlited.
86 - add a spinner state showing the user when the undo snapshotting is happening
97 - Structuted output when a skill is auto loaded for the agent
108 - Kimi-k2.5, GLM-5 and other models that use the same sdk produce these errors:
src/cli/stream-render.test.ts+67−4
@@ -42,6 +42,51 @@ function strip(s: string): string {
4242 return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
4343 }
4444
45+/**
46+ * Simulate terminal rendering: apply carriage-return + erase-line sequences
47+ * (`\r\x1b[2K`) so tests reflect what the user actually sees on screen.
48+ * This mirrors the partial-line overwrite strategy used by the stream renderer.
49+ */
50+function simulateTerminal(raw: string): string {
51+ const esc = String.fromCharCode(0x1b);
52+ // Strip all ANSI SGR color/style codes first so we can compare plain text.
53+ const noColor = raw.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
54+ // Process line-by-line, handling \r (return to line start) and \x1b[2K (erase line).
55+ const lines: string[] = [];
56+ let current = "";
57+ let i = 0;
58+ while (i < noColor.length) {
59+ const ch = noColor[i];
60+ if (ch === "\n") {
61+ lines.push(current);
62+ current = "";
63+ i++;
64+ } else if (ch === "\r") {
65+ // Carriage return: check for \x1b[2K (erase-line) immediately after.
66+ if (
67+ noColor[i + 1] === esc &&
68+ noColor[i + 2] === "[" &&
69+ noColor[i + 3] === "2" &&
70+ noColor[i + 4] === "K"
71+ ) {
72+ // Clear the current line and skip the escape sequence.
73+ current = "";
74+ i += 5;
75+ } else {
76+ // Plain \r: go to start of line but keep existing content to be
77+ // overwritten character by character. Just reset position marker.
78+ current = "";
79+ i++;
80+ }
81+ } else {
82+ current += ch;
83+ i++;
84+ }
85+ }
86+ lines.push(current);
87+ return lines.join("\n");
88+}
89+
4590 function hasAnsi(s: string, code: string): boolean {
4691 return s.includes(`${String.fromCharCode(0x1b)}${code}`);
4792 }
@@ -59,7 +104,7 @@ describe("renderTurn", () => {
59104 new Spinner(),
60105 );
61106
62- expect(strip(stdout)).toBe("◆ first line\nsecond line\n");
107+ expect(simulateTerminal(stdout)).toBe("◆ first line\nsecond line\n");
63108 expect(result).toEqual({
64109 inputTokens: 1,
65110 outputTokens: 2,
@@ -69,6 +114,20 @@ describe("renderTurn", () => {
69114 });
70115 });
71116
117+ test("preserves reply glyph on partial-line overwrite after a complete line in same delta", async () => {
118+ captureStdout();
119+
120+ await renderTurn(
121+ eventsFrom([{ type: "text-delta", delta: "hello\nworld" }, done()]),
122+ new Spinner(),
123+ );
124+
125+ // "hello\n" is flushed as a complete line. "world" is streamed as a
126+ // partial then overwritten on turn-end. The ◆ prefix appears only on
127+ // the first line; continuation lines have no prefix.
128+ expect(simulateTerminal(stdout)).toBe("◆ hello\nworld\n");
129+ });
130+
72131 test("renders buffered markdown correctly and preserves emoji across deltas", async () => {
73132 captureStdout();
74133
@@ -81,7 +140,9 @@ describe("renderTurn", () => {
81140 new Spinner(),
82141 );
83142
84- expect(strip(stdout)).toBe("◆ bold 👩🏽‍💻\n");
143+ // The partial raw text is streamed immediately and then overwritten on
144+ // turn-end with the fully styled line (bold ANSI codes applied).
145+ expect(simulateTerminal(stdout)).toBe("◆ bold 👩🏽‍💻\n");
85146 expect(hasAnsi(stdout, "[1m")).toBe(true);
86147 });
87148
@@ -116,7 +177,7 @@ describe("renderTurn", () => {
116177 { showReasoning: false },
117178 );
118179
119- expect(strip(stdout)).toBe("◆ final\n");
180+ expect(simulateTerminal(stdout)).toBe("◆ final\n");
120181 expect(result.reasoningText).toBe("step 1\nstep 2");
121182 });
122183
@@ -146,7 +207,9 @@ describe("renderTurn", () => {
146207 new Spinner(),
147208 );
148209
149- expect(strip(stdout)).toBe("· reasoning\n│ thinking\n");
210+ // The partial reasoning text is streamed raw and then overwritten on
211+ // turn-end with the properly styled (dimmed) version.
212+ expect(simulateTerminal(stdout)).toBe("· reasoning\n│ thinking\n");
150213 expect(hasAnsi(stdout, "[2m")).toBe(true);
151214 expect(result.reasoningText).toBe("thinking");
152215 });
src/cli/stream-render.ts+101−29
@@ -27,7 +27,24 @@ export async function renderTurn(
2727 const showReasoning = opts?.showReasoning ?? true;
2828 let inText = false;
2929 let inReasoning = false;
30+ // rawBuffer holds the current incomplete (no trailing \n) line being built.
3031 let rawBuffer = "";
32+ // How many characters of rawBuffer have already been written to the terminal
33+ // as a raw partial line (before markdown rendering). When a newline arrives
34+ // the partial is overwritten with the properly styled complete line.
35+ let partialWritten = 0;
36+ // Prefix shown before raw partial content during streaming previews.
37+ // For text replies: "◆ " (already written before streaming starts, so
38+ // streamPartialLine skips it). For reasoning: "│ " (written by
39+ // streamPartialLine since renderSingleLine doesn't emit a separate write).
40+ let streamPrefix = "";
41+ // Whether streamPrefix has already been written to the terminal.
42+ // True for text replies; false for reasoning (streamPartialLine emits it).
43+ let streamPrefixWritten = false;
44+ // Prefix prepended when overwriting the raw partial with the styled complete
45+ // line. For text replies: "◆ " (renderSingleLine output doesn't include it).
46+ // For reasoning: "" (renderSingleLine already includes "│ " in its output).
47+ let styledPrefix = "";
3148 let accumulatedText = "";
3249 let accumulatedReasoning = "";
3350
@@ -58,42 +75,60 @@ export async function renderTurn(
5875 return inReasoning ? `${c.dim("│ ")}${rendered.output}` : rendered.output;
5976 }
6077
61- function renderAndWrite(raw: string, endWithNewline: boolean): void {
62- // Defensive stop: some providers stream tiny chunks and spinner ticks can
63- // interleave with stdout writes unless we stop right before each render.
64- spinner.stop();
65-
66- const out = renderSingleLine(raw);
67- if (out === null) return;
68- write(out);
69- if (endWithNewline) {
70- write("\n");
71- }
78+ // Reset per-line partial-preview tracking. styledPrefix is intentionally
79+ // excluded — it is turn-level state (◆ for text, "" for reasoning) and must
80+ // persist across lines within the same turn.
81+ function resetLineState(): void {
82+ partialWritten = 0;
83+ streamPrefix = "";
84+ streamPrefixWritten = false;
7285 }
7386
87+ /**
88+ * Flush the trailing partial line (rawBuffer) and write a newline.
89+ * If a partial-line preview was already written to the terminal, overwrite
90+ * it with the properly markdown-rendered version first.
91+ */
7492 function flushAnyText(): void {
75- if (inText || inReasoning) {
76- flushAll();
77- writeln();
78- inText = false;
79- inReasoning = false;
80- reasoningBlankLineRun = 0;
93+ if (!inText && !inReasoning) return;
94+
95+ if (rawBuffer) {
96+ spinner.stop();
97+ const out = renderSingleLine(rawBuffer);
98+ rawBuffer = "";
99+ if (partialWritten > 0) {
100+ // Raw partial content is on the terminal — overwrite it with the
101+ // properly styled version.
102+ if (out !== null) write(`\r\x1b[2K${styledPrefix}${out}`);
103+ else write("\r\x1b[2K");
104+ } else {
105+ if (out !== null) write(out);
106+ }
81107 }
108+ writeln();
109+ inText = false;
110+ inReasoning = false;
111+ reasoningBlankLineRun = 0;
112+ styledPrefix = "";
113+ resetLineState();
82114 }
83115
116+ /**
117+ * Process rawBuffer: render and output all complete (newline-terminated)
118+ * lines; leave any trailing partial in rawBuffer.
119+ * If a partial-line preview was already written, the first complete line
120+ * overwrites it with `\r\x1b[2K` before its styled content.
121+ */
84122 function flushCompleteLines(): void {
85123 // Use an index cursor instead of repeatedly slicing rawBuffer after each
86- // line — the original `rawBuffer = rawBuffer.slice(boundary + 1)` pattern
87- // is O(n²) for large blocks because it copies all remaining content on
88- // every iteration. We do a single slice at the end instead.
89- // All rendered lines are also batched into one write() call to avoid the
90- // overhead of thousands of individual stdout syscalls.
124+ // line — O(n) single scan, single slice at the end.
91125 let start = 0;
92126 let boundary = rawBuffer.indexOf("\n", start);
93127 if (boundary === -1) return;
94128
95129 spinner.stop();
96130 let batchOutput = "";
131+ let firstLine = true;
97132
98133 while (boundary !== -1) {
99134 const raw = rawBuffer.slice(start, boundary);
@@ -101,19 +136,39 @@ export async function renderTurn(
101136 boundary = rawBuffer.indexOf("\n", start);
102137
103138 const out = renderSingleLine(raw);
104- if (out !== null) batchOutput += `${out}\n`;
139+ // If raw partial content was already streamed to the terminal, overwrite
140+ // it with the properly styled complete-line render.
141+ if (firstLine && partialWritten > 0) {
142+ if (out !== null) batchOutput += `\r\x1b[2K${styledPrefix}${out}\n`;
143+ else batchOutput += "\r\x1b[2K\n";
144+ } else if (out !== null) {
145+ batchOutput += `${out}\n`;
146+ }
147+ firstLine = false;
105148 }
106149
107150 rawBuffer = start > 0 ? rawBuffer.slice(start) : rawBuffer;
151+ // rawBuffer now holds a fresh partial line. Reset per-line tracking.
152+ // styledPrefix is also cleared: the reply glyph only prefixes the first
153+ // line of a turn; continuation lines have no prefix.
154+ styledPrefix = "";
155+ resetLineState();
108156 if (batchOutput) write(batchOutput);
109157 }
110158
111- function flushAll(): void {
112- if (!rawBuffer) {
113- return;
114- }
115- renderAndWrite(rawBuffer, false);
116- rawBuffer = "";
159+ /**
160+ * Write any new characters in rawBuffer that haven't been streamed yet,
161+ * giving the user immediate visual feedback before the line is complete.
162+ * Emits streamPrefix before the first characters of each new line when
163+ * it hasn't been written yet (e.g. "│ " gutter for reasoning).
164+ */
165+ function streamPartialLine(): void {
166+ if (rawBuffer.length <= partialWritten) return;
167+ spinner.stop();
168+ const prefix = !streamPrefixWritten && streamPrefix ? streamPrefix : "";
169+ write(`${prefix}${rawBuffer.slice(partialWritten)}`);
170+ streamPrefixWritten = true;
171+ partialWritten = rawBuffer.length;
117172 }
118173
119174 for await (const event of events) {
@@ -125,12 +180,20 @@ export async function renderTurn(
125180 if (!inText) {
126181 spinner.stop();
127182 write(`${G.reply} `);
183+ // "◆ " is already on the terminal. Record it for overwrite
184+ // restoration and mark the prefix as already written.
185+ streamPrefix = `${G.reply} `;
186+ styledPrefix = `${G.reply} `;
187+ streamPrefixWritten = true;
128188 inText = true;
129189 }
130190 rawBuffer += event.delta;
131191 accumulatedText += event.delta;
132192
133193 flushCompleteLines();
194+ // Stream any new partial-line content immediately so the user sees
195+ // output without waiting for the next newline.
196+ streamPartialLine();
134197 break;
135198 }
136199 case "reasoning-delta": {
@@ -146,9 +209,18 @@ export async function renderTurn(
146209 spinner.stop();
147210 writeln(`${G.info} ${c.dim("reasoning")}`);
148211 inReasoning = true;
212+ // streamPrefix is written by streamPartialLine before the first
213+ // chars of each line to show the "│ " gutter in the raw preview.
214+ // styledPrefix is empty because renderSingleLine already includes
215+ // "│ " in its output for reasoning lines.
216+ streamPrefix = c.dim("│ ");
217+ styledPrefix = "";
218+ streamPrefixWritten = false;
149219 }
150220 rawBuffer += delta;
151221 flushCompleteLines();
222+ // Stream any new partial-line content immediately.
223+ streamPartialLine();
152224 break;
153225 }