Skip to content

Commit 9d4038f

Browse files
StiensWoutclaude
andauthored
perf(acp): avoid re-encoding terminal output for every chunk (pingdotgg#19607)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent cb0553e commit 9d4038f

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
@@ -1251,6 +1251,7 @@ describe("AcpRuntimeModel", () => {
12511251
command: "pnpm test",
12521252
cwd: "/workspace",
12531253
output: "first line\nsecond line\n",
1254+
outputBytes: 23,
12541255
exitStatus: { exitCode: 0 },
12551256
});
12561257
});
@@ -1274,6 +1275,51 @@ describe("AcpRuntimeModel", () => {
12741275
expect(second.pendingOutputBytes).toBeUndefined();
12751276
});
12761277

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+
12771323
it("drops malformed and oversized ACP terminal frames without decoding them", () => {
12781324
const seeded = applyAcpAgentTerminalUpdate(undefined, {
12791325
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
@@ -50,6 +50,8 @@ export interface AcpAgentTerminalState {
5050
readonly command?: string;
5151
readonly cwd?: string;
5252
readonly output: string;
53+
/** UTF-8 byte length of `output`, so appending a chunk never re-encodes the buffer. */
54+
readonly outputBytes: number;
5355
readonly exitStatus?: EffectAcpSchema.TerminalExitStatus;
5456
readonly pendingOutputBytes?: Uint8Array;
5557
}
@@ -126,26 +128,43 @@ function decodeTerminalOutputChunk(
126128
: { text, pendingOutputBytes: Uint8Array.from(bytes.subarray(completeEnd)) };
127129
}
128130

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 };
135154
}
136155

137156
export function applyAcpAgentTerminalUpdate(
138157
previous: AcpAgentTerminalState | undefined,
139158
update: AcpAgentTerminalUpdate,
140159
): AcpAgentTerminalState {
141-
const current = previous ?? { output: "" };
160+
const current = previous ?? { output: "", outputBytes: 0 };
142161
if (update.sessionUpdate === "terminal_output_chunk") {
143162
const decoded = decodeTerminalOutputChunk(current.pendingOutputBytes, update.data);
144163
if (decoded === undefined) return current;
145164
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = current;
146165
return {
147166
...withoutPendingOutputBytes,
148-
output: truncateTerminalOutput(`${current.output}${decoded.text}`),
167+
...appendTerminalOutput(current.output, current.outputBytes, decoded.text),
149168
...(decoded.pendingOutputBytes === undefined
150169
? {}
151170
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -173,7 +192,7 @@ export function applyAcpAgentTerminalUpdate(
173192
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
174193
next = {
175194
...withoutPendingOutputBytes,
176-
output: truncateTerminalOutput(decoded.text),
195+
...appendTerminalOutput("", 0, decoded.text),
177196
...(decoded.pendingOutputBytes === undefined
178197
? {}
179198
: { pendingOutputBytes: decoded.pendingOutputBytes }),
@@ -191,7 +210,7 @@ export function applyAcpAgentTerminalUpdate(
191210
const { pendingOutputBytes: _pendingOutputBytes, ...withoutPendingOutputBytes } = next;
192211
next = {
193212
...withoutPendingOutputBytes,
194-
output: truncateTerminalOutput(`${next.output}${pendingText}`),
213+
...appendTerminalOutput(next.output, next.outputBytes, pendingText),
195214
exitStatus: update.exitStatus,
196215
};
197216
}

0 commit comments

Comments
 (0)