Skip to content

Commit b3a4e73

Browse files
StiensWoutclaude
andcommitted
fix(effect-acp): unread raw notifications no longer pin up to 32 large payloads
The raw notification stream kept the last 32 incoming notifications in a sliding queue whether or not anything read them, so a connection receiving 8 MiB notifications held about 256 MiB. Bound the unread backlog by JSON line length as well as count: keep the newest notification and as many older ones as fit in 16 MiB, still capped at 32. Line length comes from the parser, so nothing is re-encoded. Readers that keep up still receive every notification, typed handlers are unchanged, and the stream now ends when the connection terminates or its scope closes. Fixes #18799 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bf8ffe8 commit b3a4e73

4 files changed

Lines changed: 183 additions & 35 deletions

File tree

‎packages/effect-acp/src/agent.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export class AcpAgent extends Context.Service<
3636
{
3737
readonly raw: {
3838
/**
39-
* Stream of inbound ACP notifications observed on the connection.
39+
* Stream of inbound ACP notifications observed on the connection. Unread
40+
* ones are kept best-effort: the newest 32 that fit in 16 MiB of JSON,
41+
* always including the latest. Ends when the connection does.
4042
*/
4143
readonly notifications: Stream.Stream<AcpProtocol.AcpIncomingNotification>;
4244
/**

‎packages/effect-acp/src/client.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export interface AcpClientOptions {
5151
}
5252

5353
type AcpClientRaw = {
54+
/**
55+
* Inbound notifications. Unread ones are kept best-effort: the newest 32 that
56+
* fit in 16 MiB of JSON, always including the latest. Ends with the connection.
57+
*/
5458
readonly notifications: Stream.Stream<AcpProtocol.AcpIncomingNotification>;
5559
readonly request: (method: string, payload: unknown) => Effect.Effect<unknown, AcpError.AcpError>;
5660
readonly notify: (method: string, payload: unknown) => Effect.Effect<void, AcpError.AcpError>;

‎packages/effect-acp/src/protocol.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ const JsonRpcErrorResponse = Schema.Struct({
6464
});
6565
const encodeUnknownJsonString = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
6666
const encoder = new TextEncoder();
67+
const MIB = 1024 * 1024;
68+
const decodeIndex = Schema.decodeUnknownSync(Schema.Struct({ index: Schema.Number }));
69+
const largeNotification = (index: number, textLength: number) =>
70+
encoder.encode(
71+
`${encodeUnknownJsonString({
72+
jsonrpc: "2.0",
73+
method: "x/large",
74+
params: { index, text: "x".repeat(textLength) },
75+
})}\n`,
76+
);
6777
const mockPeerPath = Effect.map(Effect.service(Path.Path), (path) =>
6878
path.join(import.meta.dirname, "../test/fixtures/acp-mock-peer.ts"),
6979
);
@@ -183,6 +193,94 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
183193
}),
184194
);
185195

196+
it.effect("keeps the newest unread raw notifications that fit in 16 MiB of JSON", () =>
197+
Effect.gen(function* () {
198+
const { stdio, input } = yield* makeInMemoryStdio();
199+
const handled = yield* Queue.unbounded<number>();
200+
const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
201+
stdio,
202+
serverRequestMethods: new Set(),
203+
onNotification: (notification) =>
204+
Queue.offer(handled, decodeIndex(notification.params).index).pipe(Effect.asVoid),
205+
});
206+
207+
// Ten 3 MiB notifications: only the newest five fit in the budget.
208+
for (let index = 0; index < 10; index++) {
209+
yield* Queue.offer(input, largeNotification(index, 3 * MIB));
210+
assert.equal(yield* Queue.take(handled), index);
211+
}
212+
213+
const retained = yield* transport.incoming.pipe(Stream.take(5), Stream.runCollect);
214+
assert.deepEqual(
215+
retained.map((notification) => decodeIndex(notification.params).index),
216+
[5, 6, 7, 8, 9],
217+
);
218+
}),
219+
);
220+
221+
it.effect("keeps a single unread raw notification that alone exceeds the budget", () =>
222+
Effect.gen(function* () {
223+
const { stdio, input } = yield* makeInMemoryStdio();
224+
const handled = yield* Queue.unbounded<number>();
225+
const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
226+
stdio,
227+
serverRequestMethods: new Set(),
228+
onNotification: (notification) =>
229+
Queue.offer(handled, decodeIndex(notification.params).index).pipe(Effect.asVoid),
230+
});
231+
232+
yield* Queue.offer(input, largeNotification(0, 1));
233+
yield* Queue.take(handled);
234+
yield* Queue.offer(input, largeNotification(1, 17 * MIB));
235+
yield* Queue.take(handled);
236+
237+
const [retained] = yield* transport.incoming.pipe(Stream.take(1), Stream.runCollect);
238+
assert.equal(decodeIndex(retained?.params).index, 1);
239+
}),
240+
);
241+
242+
it.effect("delivers every raw notification to a reader that keeps up, then ends", () =>
243+
Effect.gen(function* () {
244+
const { stdio, input } = yield* makeInMemoryStdio();
245+
const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
246+
stdio,
247+
serverRequestMethods: new Set(),
248+
});
249+
const received = yield* Queue.unbounded<number>();
250+
const reader = yield* transport.incoming.pipe(
251+
Stream.map((notification) => decodeIndex(notification.params).index),
252+
Stream.tap((index) => Queue.offer(received, index)),
253+
Stream.runCollect,
254+
Effect.forkScoped,
255+
);
256+
257+
// 30 MiB in total, well past the unread budget.
258+
for (let index = 0; index < 10; index++) {
259+
yield* Queue.offer(input, largeNotification(index, 3 * MIB));
260+
assert.equal(yield* Queue.take(received), index);
261+
}
262+
yield* Queue.end(input);
263+
264+
assert.deepEqual(yield* Fiber.join(reader), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
265+
}),
266+
);
267+
268+
it.effect("ends the raw notification stream when the connection scope closes", () =>
269+
Effect.gen(function* () {
270+
const { stdio } = yield* makeInMemoryStdio();
271+
const connection = yield* Scope.make();
272+
const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
273+
stdio,
274+
serverRequestMethods: new Set(),
275+
}).pipe(Scope.provide(connection));
276+
const reader = yield* transport.incoming.pipe(Stream.runCollect, Effect.forkScoped);
277+
278+
yield* Scope.close(connection, Exit.void);
279+
280+
assert.deepEqual(yield* Fiber.join(reader), []);
281+
}),
282+
);
283+
186284
it.effect("decodes standard JSON-RPC errors into typed ACP request failures", () =>
187285
Effect.gen(function* () {
188286
const { stdio, input, output } = yield* makeInMemoryStdio();

‎packages/effect-acp/src/protocol.ts‎

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ interface AcpPendingRequest {
128128
readonly method: string;
129129
}
130130

131+
/** A decoded wire message and the length of the JSON line it arrived on. */
132+
interface AcpDecodedWireMessage {
133+
readonly message: RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded;
134+
readonly lineLength: number;
135+
}
136+
137+
interface AcpBufferedRawNotification {
138+
readonly notification: AcpIncomingNotification;
139+
readonly lineLength: number;
140+
}
141+
131142
interface AcpOutgoingWrite {
132143
readonly acknowledgement?: Deferred.Deferred<void, AcpError.AcpError>;
133144
readonly payload: string | Uint8Array;
@@ -153,19 +164,30 @@ const makeStrictNdJsonRpcParser = () => {
153164
const decoder = new TextDecoder();
154165
let buffer = "";
155166
return {
156-
decode: (bytes: Uint8Array | string): ReadonlyArray<unknown> => {
167+
// Every message in a batch line is charged the whole line.
168+
decode: (bytes: Uint8Array | string): ReadonlyArray<AcpDecodedWireMessage> => {
157169
buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true });
158170
const lines = buffer.split("\n");
159171
buffer = lines.pop() ?? "";
160-
return lines.flatMap((line) => codec.decode(line));
172+
return lines.flatMap((line) =>
173+
codec.decode(line).map((message) => ({
174+
message: normalizeAcpJsonRpcError(
175+
message as RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded,
176+
),
177+
lineLength: line.length,
178+
})),
179+
);
161180
},
162181
encode: (response: Parameters<typeof codec.encode>[0]) => {
163182
const encoded = codec.encode(response);
164183
return encoded === undefined ? undefined : `${encoded}\n`;
165184
},
166185
};
167186
};
187+
// Unread raw notifications are best-effort. The newest is always kept; older
188+
// ones are dropped once the backlog passes either limit.
168189
const MAX_BUFFERED_RAW_NOTIFICATIONS = 32;
190+
const MAX_BUFFERED_RAW_NOTIFICATION_JSON_LENGTH = 16 * 1024 * 1024;
169191
// Outbound JSON-RPC notification: no `id`, so peers never treat it as a request.
170192
const encodeJsonRpcNotification = Schema.encodeUnknownExit(
171193
Schema.fromJsonString(
@@ -213,9 +235,14 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
213235
const parser = makeStrictNdJsonRpcParser();
214236
const serverQueue = yield* Queue.unbounded<RpcMessage.FromClientEncoded>();
215237
const clientQueue = yield* Queue.unbounded<RpcMessage.FromServerEncoded>();
216-
const notificationQueue = yield* Queue.sliding<AcpIncomingNotification>(
217-
MAX_BUFFERED_RAW_NOTIFICATIONS,
218-
);
238+
const rawNotifications = yield* Queue.unbounded<AcpBufferedRawNotification, Cause.Done<void>>();
239+
// JSON length of the notifications offered since the queue was last empty.
240+
// `incoming` takes the whole backlog on every pull, so an empty queue holds
241+
// nothing, and a read can only make this an overestimate.
242+
let rawNotificationsJsonLength = 0;
243+
// Raw readers finish with the connection: termination ends the queue, and so
244+
// does closing the scope, which interrupts the reader before it terminates.
245+
yield* Effect.addFinalizer(() => Queue.end(rawNotifications));
219246
const disconnects = yield* Queue.unbounded<number>();
220247
const outgoing = yield* Queue.unbounded<AcpOutgoingWrite, AcpError.AcpError | Cause.Done<void>>();
221248
const outgoingWriterState = yield* Ref.make<AcpOutgoingWriterState>({
@@ -384,8 +411,25 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
384411
),
385412
);
386413

387-
const dispatchNotification = (notification: AcpIncomingNotification) =>
388-
Queue.offer(notificationQueue, notification).pipe(
414+
const bufferRawNotification = (notification: AcpIncomingNotification, lineLength: number) =>
415+
Effect.sync(() => {
416+
if (Queue.sizeUnsafe(rawNotifications) === 0) rawNotificationsJsonLength = 0;
417+
while (
418+
Queue.sizeUnsafe(rawNotifications) >= MAX_BUFFERED_RAW_NOTIFICATIONS ||
419+
(Queue.sizeUnsafe(rawNotifications) > 0 &&
420+
rawNotificationsJsonLength + lineLength > MAX_BUFFERED_RAW_NOTIFICATION_JSON_LENGTH)
421+
) {
422+
const dropped = Queue.takeUnsafe(rawNotifications);
423+
if (dropped === undefined || Exit.isFailure(dropped)) break;
424+
rawNotificationsJsonLength -= dropped.value.lineLength;
425+
}
426+
if (Queue.offerUnsafe(rawNotifications, { notification, lineLength })) {
427+
rawNotificationsJsonLength += lineLength;
428+
}
429+
});
430+
431+
const dispatchNotification = (notification: AcpIncomingNotification, lineLength: number) =>
432+
bufferRawNotification(notification, lineLength).pipe(
389433
Effect.andThen(
390434
// A failing or dying handler must not stop the reader, or every later
391435
// message on the connection goes unanswered.
@@ -415,6 +459,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
415459
return [
416460
Effect.gen(function* () {
417461
yield* Queue.offer(disconnects, 0);
462+
yield* Queue.end(rawNotifications);
418463
const error = yield* classify();
419464
yield* Deferred.fail(terminationFailure, error);
420465
yield* failAllExtPending(error);
@@ -499,7 +544,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
499544
);
500545
};
501546

502-
const handleRequestEncoded = (message: RpcMessage.RequestEncoded) => {
547+
const handleRequestEncoded = (message: RpcMessage.RequestEncoded, lineLength: number) => {
503548
if (message.id === "") {
504549
if (message.tag === CLIENT_METHODS.session_update) {
505550
return decodeSessionUpdate(message.payload).pipe(
@@ -518,7 +563,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
518563
cause,
519564
),
520565
),
521-
Effect.flatMap(dispatchNotification),
566+
Effect.flatMap((notification) => dispatchNotification(notification, lineLength)),
522567
);
523568
}
524569
if (
@@ -542,14 +587,13 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
542587
cause,
543588
),
544589
),
545-
Effect.flatMap(dispatchNotification),
590+
Effect.flatMap((notification) => dispatchNotification(notification, lineLength)),
546591
);
547592
}
548-
return dispatchNotification({
549-
_tag: "ExtNotification",
550-
method: message.tag,
551-
params: message.payload,
552-
});
593+
return dispatchNotification(
594+
{ _tag: "ExtNotification", method: message.tag, params: message.payload },
595+
lineLength,
596+
);
553597
}
554598

555599
const observeIncoming =
@@ -625,12 +669,13 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
625669
}),
626670
);
627671

628-
const routeDecodedMessage = (
629-
message: RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded,
630-
): Effect.Effect<void, AcpError.AcpError> => {
672+
const routeDecodedMessage = ({
673+
message,
674+
lineLength,
675+
}: AcpDecodedWireMessage): Effect.Effect<void, AcpError.AcpError> => {
631676
switch (message._tag) {
632677
case "Request":
633-
return handleRequestEncoded(message);
678+
return handleRequestEncoded(message, lineLength);
634679
case "Exit":
635680
return handleExitEncoded(message);
636681
case "Chunk":
@@ -672,25 +717,22 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
672717
).pipe(
673718
Effect.flatMap(() =>
674719
Effect.try({
675-
try: () =>
676-
(
677-
parser.decode(data) as ReadonlyArray<
678-
RpcMessage.FromClientEncoded | RpcMessage.FromServerEncoded
679-
>
680-
).map(normalizeAcpJsonRpcError),
720+
try: () => parser.decode(data),
681721
catch: (cause) =>
682722
new AcpError.AcpProtocolParseError({
683723
operation: "decode-wire-message",
684724
cause,
685725
}),
686726
}),
687727
),
688-
Effect.tap((messages) =>
689-
logProtocol({
690-
direction: "incoming",
691-
stage: "decoded",
692-
payload: messages,
693-
}),
728+
Effect.tap((decoded) =>
729+
options.logIncoming
730+
? logProtocol({
731+
direction: "incoming",
732+
stage: "decoded",
733+
payload: decoded.map(({ message }) => message),
734+
})
735+
: Effect.void,
694736
),
695737
Effect.tapErrorTag("AcpProtocolParseError", (error) =>
696738
logProtocol({
@@ -708,8 +750,8 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
708750
},
709751
}),
710752
),
711-
Effect.flatMap((messages) =>
712-
Effect.forEach(messages, routeDecodedMessage, {
753+
Effect.flatMap((decoded) =>
754+
Effect.forEach(decoded, routeDecodedMessage, {
713755
discard: true,
714756
}),
715757
),
@@ -970,7 +1012,9 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
9701012
clientProtocol,
9711013
serverProtocol,
9721014
get incoming() {
973-
return Stream.fromQueue(notificationQueue);
1015+
return Stream.fromQueue(rawNotifications).pipe(
1016+
Stream.map(({ notification }) => notification),
1017+
);
9741018
},
9751019
request: sendRequest,
9761020
notify: sendNotification,

0 commit comments

Comments
 (0)