@@ -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 }