From a51c31b59b29d477bd1c0e00a782bf2ea381a74a Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Sun, 1 Nov 2026 10:18:11 -0600 Subject: [PATCH] fix(acp): a blank line from an ACP agent no longer ends the session Co-Authored-By: Claude Opus 5.5 --- packages/effect-acp/src/protocol.test.ts | 25 ++++++++++++++++++++++++ packages/effect-acp/src/protocol.ts | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts index 7b2a923e7b02..cc49649c2704 100644 --- a/packages/effect-acp/src/protocol.test.ts +++ b/packages/effect-acp/src/protocol.test.ts @@ -310,6 +310,31 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => { }), ); + it.effect("skips blank lines between agent messages", () => + Effect.gen(function* () { + const { stdio, input } = yield* makeInMemoryStdio(); + const outcome = yield* Deferred.make(); + const transport = yield* AcpProtocol.makeAcpPatchedProtocol({ + stdio, + serverRequestMethods: new Set(), + onTermination: (error) => Deferred.succeed(outcome, error._tag).pipe(Effect.asVoid), + }); + yield* transport.incoming.pipe( + Stream.runForEach((notification) => Deferred.succeed(outcome, notification._tag)), + Effect.forkScoped, + ); + + const update = encodeUnknownJsonString({ + jsonrpc: "2.0", + method: "session/update", + params: { sessionId: "session-1", update: { sessionUpdate: "plan", entries: [] } }, + }); + yield* Queue.offer(input, encoder.encode(`\n\r\n \t\n${update}\n\n`)); + + assert.equal(yield* Deferred.await(outcome), "SessionUpdate"); + }), + ); + it.effect("logs decode failures without copying the cause or wire payload", () => Effect.gen(function* () { const secret = "acp-wire-secret-sentinel"; diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts index feb544ba12a3..b91becfe5e3e 100644 --- a/packages/effect-acp/src/protocol.ts +++ b/packages/effect-acp/src/protocol.ts @@ -162,7 +162,7 @@ const decodeElicitationComplete = Schema.decodeUnknownEffect( ); const parserFactory = RpcSerialization.ndJsonRpc(); // ndJsonRpc skips lines that are not JSON. A malformed agent line has to end the -// session, so frames are split here and each one goes through the strict codec. +// session, so frames are split here and each nonblank one goes through the strict codec. const makeStrictNdJsonRpcParser = () => { const codec = RpcSerialization.jsonRpc().makeUnsafe(); const decoder = new TextDecoder(); @@ -172,7 +172,7 @@ const makeStrictNdJsonRpcParser = () => { buffer += typeof bytes === "string" ? bytes : decoder.decode(bytes, { stream: true }); const lines = buffer.split("\n"); buffer = lines.pop() ?? ""; - return lines.flatMap((line) => codec.decode(line)); + return lines.flatMap((line) => (line.trim() === "" ? [] : codec.decode(line))); }, encode: (response: Parameters[0]) => { const encoded = codec.encode(response);