@@ -33,6 +33,9 @@ export async function renderTurn(
3333 // as a raw partial line (before markdown rendering). When a newline arrives
3434 // the partial is overwritten with the properly styled complete line.
3535 let partialWritten = 0;
36+ // Approximate visible character count shown for the streamed partial preview
37+ // (including any streamed prefix). Used to clear wrapped terminal rows.
38+ let partialVisibleChars = 0;
3639 // Prefix shown before raw partial content during streaming previews.
3740 // For text replies: "◆ " (already written before streaming starts, so
3841 // streamPartialLine skips it). For reasoning: "│ " (written by
@@ -75,11 +78,150 @@ export async function renderTurn(
7578 return inReasoning ? `${c.dim("│ ")}${rendered.output}` : rendered.output;
7679 }
7780
81+ const emojiLikeRegex = /\p{Extended_Pictographic}/u;
82+ const ansiEscapeChar = String.fromCharCode(0x1b);
83+ const graphemeSegmenter =
84+ typeof Intl !== "undefined" && typeof Intl.Segmenter === "function"
85+ ? new Intl.Segmenter(undefined, { granularity: "grapheme" })
86+ : null;
87+
88+ function stripSgrAnsi(s: string): string {
89+ if (!s) return "";
90+ let out = "";
91+ for (let i = 0; i < s.length; i++) {
92+ if (s[i] !== ansiEscapeChar || s[i + 1] !== "[") {
93+ out += s[i] ?? "";
94+ continue;
95+ }
96+
97+ let j = i + 2;
98+ while (j < s.length) {
99+ const ch = s[j] ?? "";
100+ if ((ch >= "0" && ch <= "9") || ch === ";") {
101+ j++;
102+ continue;
103+ }
104+ break;
105+ }
106+
107+ if (s[j] === "m") {
108+ i = j;
109+ continue;
110+ }
111+
112+ out += s[i] ?? "";
113+ }
114+ return out;
115+ }
116+
117+ function isControlCodePoint(cp: number): boolean {
118+ return (cp >= 0 && cp <= 0x1f) || (cp >= 0x7f && cp <= 0x9f);
119+ }
120+
121+ function isCombiningCodePoint(cp: number): boolean {
122+ return (
123+ (cp >= 0x0300 && cp <= 0x036f) ||
124+ (cp >= 0x1ab0 && cp <= 0x1aff) ||
125+ (cp >= 0x1dc0 && cp <= 0x1dff) ||
126+ (cp >= 0x20d0 && cp <= 0x20ff) ||
127+ (cp >= 0xfe20 && cp <= 0xfe2f)
128+ );
129+ }
130+
131+ function isVariationSelector(cp: number): boolean {
132+ return (cp >= 0xfe00 && cp <= 0xfe0f) || (cp >= 0xe0100 && cp <= 0xe01ef);
133+ }
134+
135+ function isWideCodePoint(cp: number): boolean {
136+ return (
137+ cp >= 0x1100 &&
138+ (cp <= 0x115f ||
139+ cp === 0x2329 ||
140+ cp === 0x232a ||
141+ (cp >= 0x2e80 && cp <= 0xa4cf && cp !== 0x303f) ||
142+ (cp >= 0xac00 && cp <= 0xd7a3) ||
143+ (cp >= 0xf900 && cp <= 0xfaff) ||
144+ (cp >= 0xfe10 && cp <= 0xfe19) ||
145+ (cp >= 0xfe30 && cp <= 0xfe6f) ||
146+ (cp >= 0xff00 && cp <= 0xff60) ||
147+ (cp >= 0xffe0 && cp <= 0xffe6) ||
148+ (cp >= 0x1f300 && cp <= 0x1f64f) ||
149+ (cp >= 0x1f900 && cp <= 0x1f9ff) ||
150+ (cp >= 0x20000 && cp <= 0x3fffd))
151+ );
152+ }
153+
154+ function graphemeWidth(grapheme: string): number {
155+ if (!grapheme) return 0;
156+ if (emojiLikeRegex.test(grapheme)) return 2;
157+
158+ let width = 0;
159+ let hasRegionalIndicator = false;
160+ for (const ch of grapheme) {
161+ const cp = ch.codePointAt(0);
162+ if (cp === undefined) continue;
163+ if (cp >= 0x1f1e6 && cp <= 0x1f1ff) hasRegionalIndicator = true;
164+ if (
165+ isControlCodePoint(cp) ||
166+ isCombiningCodePoint(cp) ||
167+ isVariationSelector(cp) ||
168+ cp === 0x200d ||
169+ (cp >= 0x1f3fb && cp <= 0x1f3ff)
170+ ) {
171+ continue;
172+ }
173+ width += isWideCodePoint(cp) ? 2 : 1;
174+ }
175+
176+ if (hasRegionalIndicator) return 2;
177+ return width;
178+ }
179+
180+ function visibleLength(s: string): number {
181+ if (!s) return 0;
182+ const plain = stripSgrAnsi(s);
183+ if (!plain) return 0;
184+
185+ if (!graphemeSegmenter) {
186+ let width = 0;
187+ for (const ch of plain) {
188+ const cp = ch.codePointAt(0);
189+ if (cp === undefined || isControlCodePoint(cp)) continue;
190+ width += isWideCodePoint(cp) ? 2 : 1;
191+ }
192+ return width;
193+ }
194+
195+ let width = 0;
196+ for (const { segment } of graphemeSegmenter.segment(plain)) {
197+ width += graphemeWidth(segment);
198+ }
199+ return width;
200+ }
201+
202+ function estimateWrappedRows(visibleChars: number): number {
203+ if (visibleChars <= 0) return 1;
204+ const cols = process.stdout.columns ?? 0;
205+ if (cols <= 0) return 1;
206+ return Math.max(1, Math.ceil(visibleChars / cols));
207+ }
208+
209+ function clearPartialPreview(): string {
210+ if (partialWritten <= 0) return "";
211+ const rows = estimateWrappedRows(partialVisibleChars);
212+ let seq = "\r\x1b[2K";
213+ for (let i = 1; i < rows; i++) {
214+ seq += "\x1b[1A\r\x1b[2K";
215+ }
216+ return seq;
217+ }
218+
78219 // Reset per-line partial-preview tracking. styledPrefix is intentionally
79220 // excluded — it is turn-level state (◆ for text, "" for reasoning) and must
80221 // persist across lines within the same turn.
81222 function resetLineState(): void {
82223 partialWritten = 0;
224+ partialVisibleChars = 0;
83225 streamPrefix = "";
84226 streamPrefixWritten = false;
85227 }
@@ -97,10 +239,11 @@ export async function renderTurn(
97239 const out = renderSingleLine(rawBuffer);
98240 rawBuffer = "";
99241 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");
242+ // Raw partial content is on the terminal — clear wrapped rows and
243+ // replace it with the properly styled version.
244+ const clearSeq = clearPartialPreview();
245+ if (out !== null) write(`${clearSeq}${styledPrefix}${out}`);
246+ else write(clearSeq);
104247 } else {
105248 if (out !== null) write(out);
106249 }
@@ -117,7 +260,7 @@ export async function renderTurn(
117260 * Process rawBuffer: render and output all complete (newline-terminated)
118261 * lines; leave any trailing partial in rawBuffer.
119262 * If a partial-line preview was already written, the first complete line
120- * overwrites it with `\r\x1b[2K` before its styled content.
263+ * clears it before writing styled content.
121264 */
122265 function flushCompleteLines(): void {
123266 // Use an index cursor instead of repeatedly slicing rawBuffer after each
@@ -136,11 +279,12 @@ export async function renderTurn(
136279 boundary = rawBuffer.indexOf("\n", start);
137280
138281 const out = renderSingleLine(raw);
139- // If raw partial content was already streamed to the terminal, overwrite
140- // it with the properly styled complete-line render.
282+ // If raw partial content was already streamed to the terminal, clear it
283+ // and replace with the properly styled complete-line render.
141284 if (firstLine && partialWritten > 0) {
142- if (out !== null) batchOutput += `\r\x1b[2K${styledPrefix}${out}\n`;
143- else batchOutput += "\r\x1b[2K\n";
285+ const clearSeq = clearPartialPreview();
286+ if (out !== null) batchOutput += `${clearSeq}${styledPrefix}${out}\n`;
287+ else batchOutput += `${clearSeq}\n`;
144288 } else if (out !== null) {
145289 batchOutput += `${out}\n`;
146290 }
@@ -165,10 +309,17 @@ export async function renderTurn(
165309 function streamPartialLine(): void {
166310 if (rawBuffer.length <= partialWritten) return;
167311 spinner.stop();
168- const prefix = !streamPrefixWritten && streamPrefix ? streamPrefix : "";
169- write(`${prefix}${rawBuffer.slice(partialWritten)}`);
170- streamPrefixWritten = true;
312+ let out = "";
313+ if (!streamPrefixWritten && streamPrefix) {
314+ out += streamPrefix;
315+ streamPrefixWritten = true;
316+ partialVisibleChars += visibleLength(streamPrefix);
317+ }
318+ const delta = rawBuffer.slice(partialWritten);
319+ out += delta;
320+ partialVisibleChars += visibleLength(delta);
171321 partialWritten = rawBuffer.length;
322+ write(out);
172323 }
173324
174325 for await (const event of events) {
@@ -181,10 +332,11 @@ export async function renderTurn(
181332 spinner.stop();
182333 write(`${G.reply} `);
183334 // "◆ " is already on the terminal. Record it for overwrite
184- // restoration and mark the prefix as already written.
335+ // restoration and count it toward wrapped-row clearing.
185336 streamPrefix = `${G.reply} `;
186337 styledPrefix = `${G.reply} `;
187338 streamPrefixWritten = true;
339+ partialVisibleChars += visibleLength(streamPrefix);
188340 inText = true;
189341 }
190342 rawBuffer += event.delta;