@devries-cameron20 / t3code-1 / commits / c2985a7cb5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(shared): fold command output without copying scrollback per chunk (#26691)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Hieu Le committed Nov 1, 2026 at 22:01 UTC c2985a7cb5a2a6cbfe2d08926148f09f22196bb2
2 files changed +50 -14
packages/shared/src/terminalOutput.test.ts
+3
@@ -52,6 +52,9 @@ describe("appendTerminalOutput", () => {
52 expect(state.text.length).toBeLessThanOrEqual(1_000);
53 expect(state.text.startsWith("line ")).toBe(true);
54 expect(state.text.endsWith("line 1999")).toBe(true);
55 + // A newline a quarter of the budget past the cut is too far to start on.
56 + expect(feed(["abcdefgh\nij\nklmnop"], 8).text).toBe("klmnop");
57 + expect(feed(["abcdefgh\nijk\nlmnop"], 8).text).toBe("jk\nlmnop");
58 });
59
60 it("stays bounded across many appends of huge output, including one endless line", () => {
packages/shared/src/terminalOutput.ts
+47 -14
@@ -17,8 +17,9 @@ interface Run {
17 type Line = ReadonlyArray<Run>;
18
19 interface TerminalScreen {
20 - /** Scrolled-out lines, each ending in newline. */
21 - readonly settled: string;
20 + /** Scrolled-out lines, each ending in newline, as head + tail. Only the tail grows, so a cut
21 + * usually slices the head instead of copying all scrollback on every chunk. */
22 + readonly settled: readonly [head: string, tail: string];
23 /** Never empty. */
24 readonly lines: ReadonlyArray<Line>;
25 readonly row: number;
@@ -31,7 +32,7 @@ export const EMPTY_TERMINAL_OUTPUT: TerminalOutputState = {
32 text: "",
33 truncated: false,
34 pending: "",
34 - screen: { settled: "", lines: [[]], row: 0, col: 0, pen: "", saved: null },
35 + screen: { settled: ["", ""], lines: [[]], row: 0, col: 0, pen: "", saved: null },
36 };
37
38 /** Default command tail, in UTF-16 code units. */
@@ -289,7 +290,8 @@ function csiParams(params: string): Array<number> {
290 }
291
292 class Terminal {
292 - settled: string;
293 + settledHead: string;
294 + settledTail: string;
295 lines: Array<Line>;
296 row: number;
297 col: number;
@@ -303,7 +305,7 @@ class Terminal {
305
306 constructor(screen: TerminalScreen, maxChars: number) {
307 this.maxChars = maxChars;
306 - this.settled = screen.settled;
308 + [this.settledHead, this.settledTail] = screen.settled;
309 this.lines = screen.lines.slice();
310 this.row = screen.row;
311 this.col = screen.col;
@@ -374,9 +376,12 @@ class Terminal {
376 }
377
378 settle(line: Line): void {
377 - this.settled += `${serializeLine(line)}\n`;
378 - if (this.settled.length > 2 * this.maxChars) {
379 - this.settled = terminalOutputTail(this.settled, this.maxChars).text;
379 + this.settledTail += `${serializeLine(line)}\n`;
380 + if (this.settledHead.length + this.settledTail.length > 2 * this.maxChars) {
381 + [this.settledHead, this.settledTail = ""] = terminalOutputTailParts(
382 + [this.settledHead, this.settledTail],
383 + this.maxChars,
384 + ).parts;
385 this.dropped = true;
386 }
387 }
@@ -435,8 +440,9 @@ class Terminal {
440 for (let row = 0; row < this.row; row += 1) this.lines[row] = [];
441 this.lines[this.row] = eraseLine(this.lines[this.row]!, this.col, 1);
442 } else if (mode === 3) {
438 - if (this.settled !== "") this.dropped = true;
439 - this.settled = "";
443 + if (this.settledHead !== "" || this.settledTail !== "") this.dropped = true;
444 + this.settledHead = "";
445 + this.settledTail = "";
446 } else {
447 this.lines = [[]];
448 this.row = 0;
@@ -541,6 +547,33 @@ export function terminalOutputTail(
547 return { text: style.prefix + text.slice(start), truncated: true };
548 }
549
550 +/**
551 + * terminalOutputTail of the parts' concatenation, kept as parts. V8 copies a concatenated string
552 + * the first time it is searched or sliced, so a newline cut slices only the part it lands in.
553 + */
554 +function terminalOutputTailParts(
555 + parts: readonly [string, ...Array<string>],
556 + maxChars: number,
557 +): { readonly parts: readonly [string, ...Array<string>]; readonly truncated: boolean } {
558 + let total = 0;
559 + for (const part of parts) total += part.length;
560 + if (total <= maxChars) return { parts, truncated: false };
561 + const start = total - maxChars;
562 + let offset = 0;
563 + for (let index = 0; index < parts.length; index += 1) {
564 + const part = parts[index]!;
565 + if (offset + part.length > start) {
566 + const newline = part.indexOf("\n", Math.max(0, start - offset));
567 + if (newline !== -1) {
568 + if (offset + newline - start >= maxChars / 4) break;
569 + return { parts: [part.slice(newline + 1), ...parts.slice(index + 1)], truncated: true };
570 + }
571 + }
572 + offset += part.length;
573 + }
574 + return { parts: [terminalOutputTail(parts.join(""), maxChars).text], truncated: true };
575 +}
576 +
577 export function appendTerminalOutput(
578 state: TerminalOutputState,
579 chunk: string,
@@ -575,15 +608,15 @@ export function appendTerminalOutput(
608 }
609 }
610 if (terminal.dropped) truncated = true;
578 - const settled = terminalOutputTail(terminal.settled, maxChars);
611 + const settled = terminalOutputTailParts([terminal.settledHead, terminal.settledTail], maxChars);
612 const window = terminal.lines.map(serializeLine).join("\n");
580 - const tail = terminalOutputTail(settled.text + window, maxChars);
613 + const tail = terminalOutputTailParts([...settled.parts, window], maxChars);
614 return {
582 - text: tail.text,
615 + text: tail.parts.reduce((text, part) => text + part, ""),
616 truncated: truncated || settled.truncated || tail.truncated,
617 pending,
618 screen: {
586 - settled: settled.text,
619 + settled: [settled.parts[0], settled.parts[1] ?? ""],
620 lines: terminal.lines,
621 row: terminal.row,
622 col: terminal.col,