Skip to content

Commit efa9c9e

Browse files
StiensWoutclaude
andcommitted
perf(provider-acp): agent terminal chunks no longer re-encode the whole output
Every ACP terminal_output_chunk re-encoded the full accumulated output to check the 16 MiB cap, so total cost grew with the square of the output. Track the retained UTF-8 byte length in the terminal state and measure only the appended text and, past the cap, the dropped prefix. Fixes #19052 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 348f217 commit efa9c9e

2 files changed

Lines changed: 75 additions & 10 deletions

File tree

‎packages/provider-acp/src/server/runtimeModel.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,7 @@ describe("AcpRuntimeModel", () => {
12681268
command: "pnpm test",
12691269
cwd: "/workspace",
12701270
output: "first line\nsecond line\n",
1271+
outputBytes: 23,
12711272
exitStatus: { exitCode: 0 },
12721273
});
12731274
});
@@ -1291,6 +1292,51 @@ describe("AcpRuntimeModel", () => {
12911292
expect(second.pendingOutputBytes).toBeUndefined();
12921293
});
12931294

1295+
it("keeps the newest 16 MiB of ACP terminal output at a UTF-8 boundary", () => {
1296+
const cap = 16 * 1024 * 1024;
1297+
const chunk = (text: string | Uint8Array) => ({
1298+
sessionUpdate: "terminal_output_chunk" as const,
1299+
terminalId: "terminal-cap",
1300+
data: Buffer.from(text).toString("base64"),
1301+
});
1302+
const seeded = applyAcpAgentTerminalUpdate(undefined, {
1303+
sessionUpdate: "terminal_update",
1304+
terminalId: "terminal-cap",
1305+
output: { data: Buffer.from(`é${"a".repeat(cap - 3)}`).toString("base64") },
1306+
});
1307+
expect(seeded.outputBytes).toBe(cap - 1);
1308+
1309+
// One byte over the cap falls inside "é", so the whole character is dropped.
1310+
const trimmed = applyAcpAgentTerminalUpdate(seeded, chunk("bc"));
1311+
expect(trimmed.output.length).toBe(cap - 1);
1312+
expect(trimmed.output.slice(0, 2)).toBe("aa");
1313+
expect(trimmed.output.slice(-3)).toBe("abc");
1314+
expect(trimmed.outputBytes).toBe(cap - 1);
1315+
1316+
const emoji = applyAcpAgentTerminalUpdate(trimmed, chunk("🙂"));
1317+
expect(emoji.output.slice(-5)).toBe("abc🙂");
1318+
expect(emoji.outputBytes).toBe(cap);
1319+
expect(Buffer.byteLength(emoji.output)).toBe(cap);
1320+
1321+
// Exit flushes an incomplete trailing character and trims again.
1322+
const pending = applyAcpAgentTerminalUpdate(emoji, chunk(Buffer.from("🙂").subarray(0, 2)));
1323+
const exited = applyAcpAgentTerminalUpdate(pending, {
1324+
sessionUpdate: "terminal_update",
1325+
terminalId: "terminal-cap",
1326+
exitStatus: { exitCode: 1 },
1327+
});
1328+
expect(exited.output.slice(-4)).toBe("c🙂\uFFFD");
1329+
expect(exited.outputBytes).toBe(Buffer.byteLength(exited.output));
1330+
expect(exited.outputBytes).toBeLessThanOrEqual(cap);
1331+
1332+
const cleared = applyAcpAgentTerminalUpdate(exited, {
1333+
sessionUpdate: "terminal_update",
1334+
terminalId: "terminal-cap",
1335+
output: null,
1336+
});
1337+
expect(cleared).toMatchObject({ output: "", outputBytes: 0, exitStatus: { exitCode: 1 } });
1338+
});
1339+
12941340
it("drops malformed and oversized ACP terminal frames without decoding them", () => {
12951341
const seeded = applyAcpAgentTerminalUpdate(undefined, {
12961342
sessionUpdate: "terminal_output_chunk",

‎packages/provider-acp/src/server/runtimeModel.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export interface AcpAgentTerminalState {
5151
readonly command?: string;
5252
readonly cwd?: string;
5353
readonly output: string;
54+
/** UTF-8 byte length of `output`, so appending a chunk never re-encodes the buffer. */
55+
readonly outputBytes: number;
5456
readonly exitStatus?: EffectAcpSchema.TerminalExitStatus;
5557
/** Incomplete trailing UTF-8 bytes retained until the next output update. */
5658
readonly pendingOutputBytes?: Uint8Array;
@@ -133,27 +135,44 @@ function decodeTerminalOutputChunk(
133135
: { text, pendingOutputBytes: Uint8Array.from(bytes.subarray(completeEnd)) };
134136
}
135137

136-
function truncateTerminalOutput(output: string): string {
137-
const bytes = Buffer.from(output, "utf8");
138-
if (bytes.length <= MAX_ACP_TERMINAL_OUTPUT_BYTES) return output;
139-
let start = bytes.length - MAX_ACP_TERMINAL_OUTPUT_BYTES;
140-
while (start < bytes.length && (bytes[start]! & 0xc0) === 0x80) start += 1;
141-
return bytes.subarray(start).toString("utf8");
138+
/**
139+
* Appends decoded text to terminal output and keeps the newest
140+
* `MAX_ACP_TERMINAL_OUTPUT_BYTES`, dropping whole leading characters. Only the
141+
* appended text and the dropped prefix are measured, so per-chunk cost does not
142+
* grow with the retained output.
143+
*/
144+
function appendTerminalOutput(
145+
output: string,
146+
outputBytes: number,
147+
text: string,
148+
): Pick<AcpAgentTerminalState, "output" | "outputBytes"> {
149+
const combined = `${output}${text}`;
150+
const combinedBytes = outputBytes + Buffer.byteLength(text, "utf8");
151+
const excess = combinedBytes - MAX_ACP_TERMINAL_OUTPUT_BYTES;
152+
if (excess <= 0) return { output: combined, outputBytes: combinedBytes };
153+
let start = 0;
154+
let dropped = 0;
155+
while (dropped < excess) {
156+
const codePoint = combined.codePointAt(start)!;
157+
dropped += codePoint < 0x80 ? 1 : codePoint < 0x800 ? 2 : codePoint < 0x10000 ? 3 : 4;
158+
start += codePoint < 0x10000 ? 1 : 2;
159+
}
160+
return { output: combined.slice(start), outputBytes: combinedBytes - dropped };
142161
}
143162

144163
/** Applies an ACP v2 agent-owned terminal snapshot or chunk to display state. */
145164
export function applyAcpAgentTerminalUpdate(
146165
previous: AcpAgentTerminalState | undefined,
147166
update: AcpAgentTerminalUpdate,
148167
): AcpAgentTerminalState {
149-
const current = previous ?? { output: "" };
168+
const current = previous ?? { output: "", outputBytes: 0 };
150169
if (update.sessionUpdate === "terminal_output_chunk") {
151170
const decoded = decodeTerminalOutputChunk(current.pendingOutputBytes, update.data);
152171
if (decoded === undefined) return current;
153172
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = current;
154173
return {
155174
...withoutPendingOutputBytes,
156-
output: truncateTerminalOutput(`${current.output}${decoded.text}`),
175+
...appendTerminalOutput(current.output, current.outputBytes, decoded.text),
157176
...(decoded.pendingOutputBytes === undefined
158177
? {}
159178
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -181,7 +200,7 @@ export function applyAcpAgentTerminalUpdate(
181200
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
182201
next = {
183202
...withoutPendingOutputBytes,
184-
output: truncateTerminalOutput(decoded.text),
203+
...appendTerminalOutput("", 0, decoded.text),
185204
...(decoded.pendingOutputBytes === undefined
186205
? {}
187206
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -199,7 +218,7 @@ export function applyAcpAgentTerminalUpdate(
199218
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
200219
next = {
201220
...withoutPendingOutputBytes,
202-
output: truncateTerminalOutput(`${next.output}${pendingText}`),
221+
...appendTerminalOutput(next.output, next.outputBytes, pendingText),
203222
exitStatus: update.exitStatus,
204223
};
205224
}

0 commit comments

Comments
 (0)