The best way to control Claude Code, Codex and more from any computer or phone
perf(acp): avoid re-encoding terminal output for every chunk (#19607)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Wout Stiens committed
Oct 20, 2026 at 01:55 UTC
9d4038f73189aeaf4389e6f43d80910c1718b7b9
2 files changed
+75
-10
packages/provider-acp/src/server/runtimeModel.test.ts
+46
@@ -1251,6 +1251,7 @@ describe("AcpRuntimeModel", () => {
1251
command: "pnpm test",
1252
cwd: "/workspace",
1253
output: "first line\nsecond line\n",
1254
+ outputBytes: 23,
1255
exitStatus: { exitCode: 0 },
1256
});
1257
});
@@ -1274,6 +1275,51 @@ describe("AcpRuntimeModel", () => {
1275
expect(second.pendingOutputBytes).toBeUndefined();
1276
});
1277
1278
+ it("keeps the newest 16 MiB of ACP terminal output at a UTF-8 boundary", () => {
1279
+ const cap = 16 * 1024 * 1024;
1280
+ const chunk = (text: string | Uint8Array) => ({
1281
+ sessionUpdate: "terminal_output_chunk" as const,
1282
+ terminalId: "terminal-cap",
1283
+ data: Buffer.from(text).toString("base64"),
1284
+ });
1285
+ const seeded = applyAcpAgentTerminalUpdate(undefined, {
1286
+ sessionUpdate: "terminal_update",
1287
+ terminalId: "terminal-cap",
1288
+ output: { data: Buffer.from(`é${"a".repeat(cap - 3)}`).toString("base64") },
1289
+ });
1290
+ expect(seeded.outputBytes).toBe(cap - 1);
1291
+
1292
+ // One byte over the cap falls inside "é", so the whole character is dropped.
1293
+ const trimmed = applyAcpAgentTerminalUpdate(seeded, chunk("bc"));
1294
+ expect(trimmed.output.length).toBe(cap - 1);
1295
+ expect(trimmed.output.slice(0, 2)).toBe("aa");
1296
+ expect(trimmed.output.slice(-3)).toBe("abc");
1297
+ expect(trimmed.outputBytes).toBe(cap - 1);
1298
+
1299
+ const emoji = applyAcpAgentTerminalUpdate(trimmed, chunk("🙂"));
1300
+ expect(emoji.output.slice(-5)).toBe("abc🙂");
1301
+ expect(emoji.outputBytes).toBe(cap);
1302
+ expect(Buffer.byteLength(emoji.output)).toBe(cap);
1303
+
1304
+ // Exit flushes an incomplete trailing character and trims again.
1305
+ const pending = applyAcpAgentTerminalUpdate(emoji, chunk(Buffer.from("🙂").subarray(0, 2)));
1306
+ const exited = applyAcpAgentTerminalUpdate(pending, {
1307
+ sessionUpdate: "terminal_update",
1308
+ terminalId: "terminal-cap",
1309
+ exitStatus: { exitCode: 1 },
1310
+ });
1311
+ expect(exited.output.slice(-4)).toBe("c🙂\uFFFD");
1312
+ expect(exited.outputBytes).toBe(Buffer.byteLength(exited.output));
1313
+ expect(exited.outputBytes).toBeLessThanOrEqual(cap);
1314
+
1315
+ const cleared = applyAcpAgentTerminalUpdate(exited, {
1316
+ sessionUpdate: "terminal_update",
1317
+ terminalId: "terminal-cap",
1318
+ output: null,
1319
+ });
1320
+ expect(cleared).toMatchObject({ output: "", outputBytes: 0, exitStatus: { exitCode: 1 } });
1321
+ });
1322
+
1323
it("drops malformed and oversized ACP terminal frames without decoding them", () => {
1324
const seeded = applyAcpAgentTerminalUpdate(undefined, {
1325
sessionUpdate: "terminal_output_chunk",
packages/provider-acp/src/server/runtimeModel.ts
+29
-10
@@ -50,6 +50,8 @@ export interface AcpAgentTerminalState {
50
readonly command?: string;
51
readonly cwd?: string;
52
readonly output: string;
53
+ /** UTF-8 byte length of `output`, so appending a chunk never re-encodes the buffer. */
54
+ readonly outputBytes: number;
55
readonly exitStatus?: EffectAcpSchema.TerminalExitStatus;
56
readonly pendingOutputBytes?: Uint8Array;
57
}
@@ -126,26 +128,43 @@ function decodeTerminalOutputChunk(
128
: { text, pendingOutputBytes: Uint8Array.from(bytes.subarray(completeEnd)) };
129
}
130
129
-function truncateTerminalOutput(output: string): string {
130
- const bytes = Buffer.from(output, "utf8");
131
- if (bytes.length <= MAX_ACP_TERMINAL_OUTPUT_BYTES) return output;
132
- let start = bytes.length - MAX_ACP_TERMINAL_OUTPUT_BYTES;
133
- while (start < bytes.length && (bytes[start]! & 0xc0) === 0x80) start += 1;
134
- return bytes.subarray(start).toString("utf8");
131
+/**
132
+ * Appends decoded text to terminal output and keeps the newest
133
+ * `MAX_ACP_TERMINAL_OUTPUT_BYTES`, dropping whole leading characters. Only the
134
+ * appended text and the dropped prefix are measured, so per-chunk cost does not
135
+ * grow with the retained output.
136
+ */
137
+function appendTerminalOutput(
138
+ output: string,
139
+ outputBytes: number,
140
+ text: string,
141
+): Pick<AcpAgentTerminalState, "output" | "outputBytes"> {
142
+ const combined = `${output}${text}`;
143
+ const combinedBytes = outputBytes + Buffer.byteLength(text, "utf8");
144
+ const excess = combinedBytes - MAX_ACP_TERMINAL_OUTPUT_BYTES;
145
+ if (excess <= 0) return { output: combined, outputBytes: combinedBytes };
146
+ let start = 0;
147
+ let dropped = 0;
148
+ while (dropped < excess) {
149
+ const codePoint = combined.codePointAt(start)!;
150
+ dropped += codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4;
151
+ start += codePoint < 0x10000 ? 1 : 2;
152
+ }
153
+ return { output: combined.slice(start), outputBytes: combinedBytes - dropped };
154
}
155
156
export function applyAcpAgentTerminalUpdate(
157
previous: AcpAgentTerminalState | undefined,
158
update: AcpAgentTerminalUpdate,
159
): AcpAgentTerminalState {
141
- const current = previous ?? { output: "" };
160
+ const current = previous ?? { output: "", outputBytes: 0 };
161
if (update.sessionUpdate === "terminal_output_chunk") {
162
const decoded = decodeTerminalOutputChunk(current.pendingOutputBytes, update.data);
163
if (decoded === undefined) return current;
164
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = current;
165
return {
166
...withoutPendingOutputBytes,
148
- output: truncateTerminalOutput(`${current.output}${decoded.text}`),
167
+ ...appendTerminalOutput(current.output, current.outputBytes, decoded.text),
168
...(decoded.pendingOutputBytes === undefined
169
? {}
170
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -173,7 +192,7 @@ export function applyAcpAgentTerminalUpdate(
192
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
193
next = {
194
...withoutPendingOutputBytes,
176
- output: truncateTerminalOutput(decoded.text),
195
+ ...appendTerminalOutput("", 0, decoded.text),
196
...(decoded.pendingOutputBytes === undefined
197
? {}
198
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -191,7 +210,7 @@ export function applyAcpAgentTerminalUpdate(
210
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
211
next = {
212
...withoutPendingOutputBytes,
194
- output: truncateTerminalOutput(`${next.output}${pendingText}`),
213
+ ...appendTerminalOutput(next.output, next.outputBytes, pendingText),
214
exitStatus: update.exitStatus,
215
};
216
}