xonecas.io
64875
Mc

mini-coder / commit 05e167e

perf(cli): avoid redundant redraws for plain streamed lines

xonecas2026-03-14 05e167e274a61d20f2d0f385a78aa1959e502a6c parent 7dd4a2d

2 files changed

src/cli/stream-render-content.ts+31−14
@@ -79,15 +79,18 @@ export class StreamRenderContent {
7979
8080 if (this.rawBuffer) {
8181 this.spinner.stop();
82- const out = this.renderSingleLine(this.rawBuffer);
82+ const raw = this.rawBuffer;
83+ const out = this.renderSingleLine(raw);
8384 this.rawBuffer = "";
8485 if (this.partialPreview.partialWritten > 0) {
85- const clearSeq = buildClearPartialPreview(
86- this.partialPreview,
87- terminal.stdoutColumns,
88- );
89- if (out !== null) write(`${clearSeq}${this.styledPrefix}${out}`);
90- else write(clearSeq);
86+ if (!this.canKeepStreamedPartial(raw, out)) {
87+ const clearSeq = buildClearPartialPreview(
88+ this.partialPreview,
89+ terminal.stdoutColumns,
90+ );
91+ if (out !== null) write(`${clearSeq}${this.styledPrefix}${out}`);
92+ else write(clearSeq);
93+ }
9194 } else if (out !== null) {
9295 write(out);
9396 }
@@ -101,6 +104,14 @@ export class StreamRenderContent {
101104 this.resetLineState();
102105 }
103106
107+ private canKeepStreamedPartial(raw: string, out: string | null): boolean {
108+ return (
109+ this.inText &&
110+ out === raw &&
111+ this.partialPreview.partialWritten === raw.length
112+ );
113+ }
114+
104115 private renderSingleLine(raw: string): string | null {
105116 const source = this.inReasoning ? raw.replace(/[ \t]+$/g, "") : raw;
106117 if (this.inReasoning && source.trim() === "") {
@@ -138,13 +149,19 @@ export class StreamRenderContent {
138149
139150 const out = this.renderSingleLine(raw);
140151 if (firstLine && this.partialPreview.partialWritten > 0) {
141- const clearSeq = buildClearPartialPreview(
142- this.partialPreview,
143- terminal.stdoutColumns,
144- );
145- if (out !== null)
146- batchOutput += `${clearSeq}${this.styledPrefix}${out}\n`;
147- else batchOutput += `${clearSeq}\n`;
152+ if (this.canKeepStreamedPartial(raw, out)) {
153+ batchOutput += "\n";
154+ } else {
155+ const clearSeq = buildClearPartialPreview(
156+ this.partialPreview,
157+ terminal.stdoutColumns,
158+ );
159+ if (out !== null) {
160+ batchOutput += `${clearSeq}${this.styledPrefix}${out}\n`;
161+ } else {
162+ batchOutput += `${clearSeq}\n`;
163+ }
164+ }
148165 } else if (out !== null) {
149166 batchOutput += `${out}\n`;
150167 }
src/cli/stream-render.test.ts+18−6
@@ -159,6 +159,18 @@ describe("renderTurn", () => {
159159 });
160160 });
161161
162+ test("keeps plain streamed partial without clear/rewrite on turn end", async () => {
163+ captureStdout();
164+
165+ await renderTurn(
166+ eventsFrom([{ type: "text-delta", delta: "plain text" }, done()]),
167+ new Spinner(),
168+ );
169+
170+ expect(stdout.includes("\r\x1b[2K")).toBe(false);
171+ expect(simulateTerminal(stdout)).toBe("◆ plain text\n");
172+ });
173+
162174 test("preserves reply glyph on partial-line overwrite after a complete line in same delta", async () => {
163175 captureStdout();
164176
@@ -173,7 +185,7 @@ describe("renderTurn", () => {
173185 expect(simulateTerminal(stdout)).toBe("◆ hello\nworld\n");
174186 });
175187
176- test("clears wrapped streamed partials before rendering final line", async () => {
188+ test("clears wrapped streamed partials when trailing content arrives in a later chunk", async () => {
177189 captureStdout();
178190 await withTerminalColumns(20, async () => {
179191 await renderTurn(
@@ -198,7 +210,7 @@ describe("renderTurn", () => {
198210 ).toBe(1);
199211 });
200212
201- test("counts initial reply prefix when clearing near wrap boundary", async () => {
213+ test("keeps wrapped partial prefix accounting without extra clear operations", async () => {
202214 captureStdout();
203215 await withTerminalColumns(20, async () => {
204216 await renderTurn(
@@ -211,8 +223,8 @@ describe("renderTurn", () => {
211223 );
212224 });
213225
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);
226+ expect(stdout.includes("\r\x1b[2K\x1b[1A\r\x1b[2K")).toBe(false);
227+ expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(0);
216228 expect(simulateTerminal(stdout)).toContain("◆ 1234567890123456789\n");
217229 });
218230
@@ -233,7 +245,7 @@ describe("renderTurn", () => {
233245 expect(simulateTerminal(stdout)).toContain("◆ 👩🏽‍💻abc\n");
234246 });
235247
236- test("clears extra wrapped row for wide CJK characters", async () => {
248+ test("does not clear wrapped rows for wide CJK characters when styling is unchanged", async () => {
237249 captureStdout();
238250 await withTerminalColumns(10, async () => {
239251 await renderTurn(
@@ -246,7 +258,7 @@ describe("renderTurn", () => {
246258 );
247259 });
248260
249- expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(1);
261+ expect(countOccurrences(stdout, "\x1b[1A\r\x1b[2K")).toBe(0);
250262 expect(simulateTerminal(stdout)).toContain("◆ 你好你好a\n");
251263 });