The best way to control Claude Code, Codex and more from any computer or phone
perf(effect-acp): frame large ACP stdout lines in linear time (#26686)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Hieu Le committed
Nov 1, 2026 at 22:01 UTC
4d7cb9bc2ee7f44ac20ad29746bf56c1dc742855
2 files changed
+40
-32
packages/effect-acp/src/protocol.test.ts
+31
-29
@@ -108,37 +108,39 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
108
},
109
});
110
111
- yield* Queue.offer(
112
- input,
113
- yield* encodeJsonl(SessionUpdateNotification, {
114
- jsonrpc: "2.0",
115
- method: "session/update",
116
- params: {
117
- sessionId: "session-1",
118
- update: {
119
- sessionUpdate: "plan",
120
- entries: [
121
- {
122
- content: "Inspect repository",
123
- priority: "high",
124
- status: "in_progress",
125
- },
126
- ],
127
- },
111
+ const updateFrame = yield* encodeJsonl(SessionUpdateNotification, {
112
+ jsonrpc: "2.0",
113
+ method: "session/update",
114
+ params: {
115
+ sessionId: "session-1",
116
+ update: {
117
+ sessionUpdate: "plan",
118
+ entries: [
119
+ {
120
+ content: "Inspect repository",
121
+ priority: "high",
122
+ status: "in_progress",
123
+ },
124
+ ],
125
},
129
- }),
130
- );
126
+ },
127
+ });
128
+ const completionFrame = yield* encodeJsonl(ElicitationCompleteNotification, {
129
+ jsonrpc: "2.0",
130
+ method: "elicitation/complete",
131
+ params: {
132
+ elicitationId: "elicitation-1",
133
+ },
134
+ });
135
132
- yield* Queue.offer(
133
- input,
134
- yield* encodeJsonl(ElicitationCompleteNotification, {
135
- jsonrpc: "2.0",
136
- method: "elicitation/complete",
137
- params: {
138
- elicitationId: "elicitation-1",
139
- },
140
- }),
141
- );
136
+ // Frames split across reads, with one read ending one frame and starting the next.
137
+ const wire = new Uint8Array([...updateFrame, ...completionFrame]);
138
+ const splits = [10, updateFrame.length + 10, wire.length];
139
+ let start = 0;
140
+ for (const end of splits) {
141
+ yield* Queue.offer(input, wire.subarray(start, end));
142
+ start = end;
143
+ }
144
145
const [update, completion] = yield* Deferred.await(notifications);
146
assert.equal(update?._tag, "SessionUpdate");
packages/effect-acp/src/protocol.ts
+9
-3
@@ -169,9 +169,15 @@ const makeStrictNdJsonRpcParser = () => {
169
let buffer = "";
170
return {
171
decode: (bytes: Uint8Array | string): ReadonlyArray<unknown> => {
172
- buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
173
- const lines = buffer.split("\n");
174
- buffer = lines.pop() ?? "";
172
+ const chunk = typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
173
+ const lastNewline = chunk.lastIndexOf("\n");
174
+ // Splitting the whole buffer per chunk is quadratic for multi-megabyte lines.
175
+ if (lastNewline === -1) {
176
+ buffer += chunk;
177
+ return [];
178
+ }
179
+ const lines = (buffer + chunk.slice(0, lastNewline)).split("\n");
180
+ buffer = chunk.slice(lastNewline + 1);
181
return lines.flatMap((line) => codec.decode(line));
182
},
183
encode: (response: Parameters<typeof codec.encode>[0]) => {