xonecas.io
64875
Mc

mini-coder / commit 98c082f

fix(cli): correct streamed partial clearing for wrapped unicode output

xonecas2026-03-14 98c082fda8b34aeab8ef95059a7605b6bc26bd79 parent e414e12

3 files changed

TODO.md+22−0
@@ -86,6 +86,28 @@ AI_APICallError: Expected dict in tool call arguments, got <class 'str'>
8686 ◆ Subagent parallelism hit a SQLite lock in the shared app DB, so I’m finishing the audit with direct reads and a smaller follow-up pass grounded in file references.
8787 ```
8888
89+- double line print bug:
90+
91+```
92+· reasoning
93+│ Mapping command definitions
94+◆ Progress update: I’ve loaded the idea/TODO and core user docs; next I’m scanning implementation files to map actual behavior against t
95+◆ Progress update: I’ve loaded the idea/TODO and core user docs; next I’m scanning implementation files to map actual behavior against those sources and capture precise gaps/inconsistencies for the report.
96+```
97+
98+- read tool path issues, says file not found on valid path:
99+
100+```
101+· reasoning
102+│ Investigating duplicate progress lines
103+ ← read ~/src/mini-coder/src/cli/stream-render.ts:1+360
104+ ✖ File not found: ~/src/mini-coder/src/cli/stream-render.ts
105+· reasoning
106+│ Deciding relative path usage
107+ ← read src/cli/stream-render.ts:1+380
108+ · src/cli/stream-render.ts 296 lines
109+```
110+
89111 ---
90112
91113 ## UI Audit
src/cli/stream-render.test.ts+150−28
@@ -43,47 +43,62 @@ function strip(s: string): string {
4343 }
4444
4545 /**
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.
46+ * Simulate terminal rendering for key cursor-control sequences used by the
47+ * stream renderer: `\r`, `\n`, `\x1b[2K` (erase line), and `\x1b[1A` (cursor up).
4948 */
5049 function simulateTerminal(raw: string): string {
5150 const esc = String.fromCharCode(0x1b);
52- // Strip all ANSI SGR color/style codes first so we can compare plain text.
51+ // Strip ANSI SGR color/style codes first so we can compare plain text.
5352 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 = "";
53+ const lines: string[] = [""];
54+ let row = 0;
55+ let col = 0;
5756 let i = 0;
57+
58+ const ensureRow = (idx: number): void => {
59+ while (lines.length <= idx) lines.push("");
60+ };
61+
5862 while (i < noColor.length) {
5963 const ch = noColor[i];
6064 if (ch === "\n") {
61- lines.push(current);
62- current = "";
65+ row++;
66+ ensureRow(row);
67+ col = 0;
68+ i++;
69+ continue;
70+ }
71+ if (ch === "\r") {
72+ col = 0;
6373 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++;
74+ continue;
75+ }
76+ if (ch === esc && noColor[i + 1] === "[") {
77+ if (noColor[i + 2] === "2" && noColor[i + 3] === "K") {
78+ lines[row] = "";
79+ col = 0;
80+ i += 4;
81+ continue;
82+ }
83+ if (noColor[i + 2] === "1" && noColor[i + 3] === "A") {
84+ row = Math.max(0, row - 1);
85+ col = Math.min(col, lines[row]?.length ?? 0);
86+ i += 4;
87+ continue;
8088 }
89+ }
90+
91+ ensureRow(row);
92+ const line = lines[row] ?? "";
93+ if (col >= line.length) {
94+ lines[row] = line + ch;
8195 } else {
82- current += ch;
83- i++;
96+ lines[row] = `${line.slice(0, col)}${ch}${line.slice(col + 1)}`;
8497 }
98+ col++;
99+ i++;
85100 }
86- lines.push(current);
101+
87102 return lines.join("\n");
88103 }
89104
@@ -91,6 +106,36 @@ function hasAnsi(s: string, code: string): boolean {
91106 return s.includes(`${String.fromCharCode(0x1b)}${code}`);
92107 }
93108
109+async function withTerminalColumns(
110+ cols: number,
111+ fn: () => Promise<void>,
112+): Promise<void> {
113+ const out = process.stdout as NodeJS.WriteStream & { columns?: number };
114+ const previous = Object.getOwnPropertyDescriptor(out, "columns");
115+ Object.defineProperty(out, "columns", {
116+ value: cols,
117+ configurable: true,
118+ writable: true,
119+ });
120+ try {
121+ await fn();
122+ } finally {
123+ if (previous) {
124+ Object.defineProperty(out, "columns", previous);
125+ } else {
126+ Object.defineProperty(out, "columns", {
127+ value: undefined,
128+ configurable: true,
129+ writable: true,
130+ });
131+ }
132+ }
133+}
134+
135+function countOccurrences(haystack: string, needle: string): number {
136+ return haystack.split(needle).length - 1;
137+}
138+
94139 describe("renderTurn", () => {
95140 test("renders complete lines immediately and flushes the trailing partial line", async () => {
96141 captureStdout();
@@ -128,6 +173,83 @@ describe("renderTurn", () => {
128173 expect(simulateTerminal(stdout)).toBe("◆ hello\nworld\n");
129174 });
130175
176+ test("clears wrapped streamed partials before rendering final line", async () => {
177+ captureStdout();
178+ await withTerminalColumns(20, async () => {
179+ await renderTurn(
180+ eventsFrom([
181+ { type: "text-delta", delta: "this is a long streamed partial" },
182+ { type: "text-delta", delta: " line\n" },
183+ done(),
184+ ]),
185+ new Spinner(),
186+ );
187+ });
188+
189+ expect(stdout.includes("\x1b[1A\r\x1b[2K")).toBe(true);
190+ expect(simulateTerminal(stdout)).toContain(
191+ "◆ this is a long streamed partial line\n",
192+ );
193+ expect(
194+ countOccurrences(
195+ simulateTerminal(stdout),
196+ "this is a long streamed partial line",
197+ ),
198+ ).toBe(1);
199+ });
200+
201+ test("counts initial reply prefix when clearing near wrap boundary", async () => {
202+ captureStdout();
203+ await withTerminalColumns(20, async () => {
204+ await renderTurn(
205+ eventsFrom([
206+ { type: "text-delta", delta: "1234567890123456789" },
207+ { type: "text-delta", delta: "\n" },
208+ done(),
209+ ]),
210+ new Spinner(),
211+ );
212+ });
213+
214+ expect(stdout.includes("\r\x1b[2K\x1b[1A\r\x1b[2K")).toBe(true);
215+ expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(1);
216+ expect(simulateTerminal(stdout)).toContain("◆ 1234567890123456789\n");
217+ });
218+
219+ test("does not over-clear wrapped rows for emoji grapheme clusters", async () => {
220+ captureStdout();
221+ await withTerminalColumns(8, async () => {
222+ await renderTurn(
223+ eventsFrom([
224+ { type: "text-delta", delta: "👩🏽‍💻abc" },
225+ { type: "text-delta", delta: "\n" },
226+ done(),
227+ ]),
228+ new Spinner(),
229+ );
230+ });
231+
232+ expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(0);
233+ expect(simulateTerminal(stdout)).toContain("◆ 👩🏽‍💻abc\n");
234+ });
235+
236+ test("clears extra wrapped row for wide CJK characters", async () => {
237+ captureStdout();
238+ await withTerminalColumns(10, async () => {
239+ await renderTurn(
240+ eventsFrom([
241+ { type: "text-delta", delta: "你好你好a" },
242+ { type: "text-delta", delta: "\n" },
243+ done(),
244+ ]),
245+ new Spinner(),
246+ );
247+ });
248+
249+ expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(1);
250+ expect(simulateTerminal(stdout)).toContain("◆ 你好你好a\n");
251+ });
252+
131253 test("renders buffered markdown correctly and preserves emoji across deltas", async () => {
132254 captureStdout();
133255
src/cli/stream-render.ts+165−13
@@ -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;