The best way to control Claude Code, Codex and more from any computer or phone
perf(acp): long agent terminal output no longer ties up the server (#24479)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
Victor Vogel committed
Oct 31, 2026 at 03:38 UTC
146dd04890d297300c1c75cfbcb6776a42197f8f
2 files changed
+27
-21
packages/provider-acp/src/server/runtimeModel.test.ts
+24
-14
@@ -1100,8 +1100,9 @@ describe("AcpRuntimeModel", () => {
1100
expect(second.pendingOutputBytes).toBeUndefined();
1101
});
1102
1103
- it("keeps the newest 16 MiB of ACP terminal output at a UTF-8 boundary", () => {
1103
+ it("trims full ACP terminal output by a further 1 MiB at a UTF-8 boundary", () => {
1104
const cap = 16 * 1024 * 1024;
1105
+ const step = 1024 * 1024;
1106
const chunk = (text: string | Uint8Array) => ({
1107
sessionUpdate: "terminal_output_chunk" as const,
1108
terminalId: "terminal-cap",
@@ -1110,32 +1111,41 @@ describe("AcpRuntimeModel", () => {
1111
const seeded = applyAcpAgentTerminalUpdate(undefined, {
1112
sessionUpdate: "terminal_update",
1113
terminalId: "terminal-cap",
1113
- output: { data: Buffer.from(`é${"a".repeat(cap - 3)}`).toString("base64") },
1114
+ output: {
1115
+ data: Buffer.from(`${"a".repeat(step)}é${"a".repeat(cap - step - 3)}`).toString("base64"),
1116
+ },
1117
});
1118
expect(seeded.outputBytes).toBe(cap - 1);
1119
1117
- // One byte over the cap falls inside "é", so the whole character is dropped.
1118
- const trimmed = applyAcpAgentTerminalUpdate(seeded, chunk("bc"));
1119
- expect(trimmed.output.length).toBe(cap - 1);
1120
+ const full = applyAcpAgentTerminalUpdate(seeded, chunk("b"));
1121
+ expect(full.outputBytes).toBe(cap);
1122
+ expect(full.output.length).toBe(cap - 1);
1123
+
1124
+ // The cut at one byte past the cap plus 1 MiB falls inside "é", so the whole character goes.
1125
+ const trimmed = applyAcpAgentTerminalUpdate(full, chunk("c"));
1126
+ expect(trimmed.outputBytes).toBe(cap - step - 1);
1127
+ expect(trimmed.output.length).toBe(cap - step - 1);
1128
expect(trimmed.output.slice(0, 2)).toBe("aa");
1129
expect(trimmed.output.slice(-3)).toBe("abc");
1122
- expect(trimmed.outputBytes).toBe(cap - 1);
1130
1124
- const emoji = applyAcpAgentTerminalUpdate(trimmed, chunk("🙂"));
1125
- expect(emoji.output.slice(-5)).toBe("abc🙂");
1126
- expect(emoji.outputBytes).toBe(cap);
1127
- expect(Buffer.byteLength(emoji.output)).toBe(cap);
1131
+ const appended = applyAcpAgentTerminalUpdate(trimmed, chunk("🙂"));
1132
+ expect(appended.output.slice(-5)).toBe("abc🙂");
1133
+ expect(appended.output.length).toBe(trimmed.output.length + 2);
1134
+ expect(appended.outputBytes).toBe(cap - step + 3);
1135
+
1136
+ expect(Buffer.byteLength(appended.output)).toBe(appended.outputBytes);
1137
1129
- // Exit flushes an incomplete trailing character and trims again.
1130
- const pending = applyAcpAgentTerminalUpdate(emoji, chunk(Buffer.from("🙂").subarray(0, 2)));
1138
+ const refilled = applyAcpAgentTerminalUpdate(appended, chunk("x".repeat(step - 3)));
1139
+ expect(refilled.outputBytes).toBe(cap);
1140
+ const pending = applyAcpAgentTerminalUpdate(refilled, chunk(Buffer.from("🙂").subarray(0, 2)));
1141
const exited = applyAcpAgentTerminalUpdate(pending, {
1142
sessionUpdate: "terminal_update",
1143
terminalId: "terminal-cap",
1144
exitStatus: { exitCode: 1 },
1145
});
1136
- expect(exited.output.slice(-4)).toBe("c🙂\uFFFD");
1146
+ expect(exited.output.slice(-4)).toBe("xxx\uFFFD");
1147
+ expect(exited.outputBytes).toBe(cap - step);
1148
expect(exited.outputBytes).toBe(Buffer.byteLength(exited.output));
1138
- expect(exited.outputBytes).toBeLessThanOrEqual(cap);
1149
1150
const cleared = applyAcpAgentTerminalUpdate(exited, {
1151
sessionUpdate: "terminal_update",
packages/provider-acp/src/server/runtimeModel.ts
+3
-7
@@ -62,6 +62,7 @@ type AcpAgentTerminalUpdate = Extract<
62
>;
63
64
const MAX_ACP_TERMINAL_OUTPUT_BYTES = 16 * 1024 * 1024;
65
+const ACP_TERMINAL_OUTPUT_TRIM_BYTES = 1024 * 1024;
66
const MAX_ACP_TERMINAL_FRAME_BYTES = MAX_ACP_TERMINAL_OUTPUT_BYTES;
67
68
function isBase64DataCode(code: number): boolean {
@@ -128,12 +129,7 @@ function decodeTerminalOutputChunk(
129
: { text, pendingOutputBytes: Uint8Array.from(bytes.subarray(completeEnd)) };
130
}
131
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
- */
132
+// Drop an extra batch at UTF-8 boundaries so flattening the retained string is amortized.
133
function appendTerminalOutput(
134
output: string,
135
outputBytes: number,
@@ -145,7 +141,7 @@ function appendTerminalOutput(
141
if (excess <= 0) return { output: combined, outputBytes: combinedBytes };
142
let start = 0;
143
let dropped = 0;
148
- while (dropped < excess) {
144
+ while (dropped < excess + ACP_TERMINAL_OUTPUT_TRIM_BYTES) {
145
const codePoint = combined.codePointAt(start)!;
146
dropped += codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4;
147
start += codePoint < 0x10000 ? 1 : 2;