diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts index 19f58292ac46..6469b2ce5dbe 100644 --- a/packages/effect-acp/src/protocol.test.ts +++ b/packages/effect-acp/src/protocol.test.ts @@ -531,6 +531,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 490c27d8e0b0..d8745bbfe7b1 100644 --- a/packages/effect-acp/src/protocol.ts +++ b/packages/effect-acp/src/protocol.ts @@ -173,7 +173,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(); @@ -197,7 +197,7 @@ const makeStrictNdJsonRpcParser = () => { } const lines = (buffer + chunk.slice(0, lastNewline)).split("\n"); buffer = chunk.slice(lastNewline + 1); - return lines.flatMap(decodeLine); + return lines.flatMap((line) => (line.trim() === "" ? [] : decodeLine(line))); }, // A truncated final line is left to the exit status, which explains a crash better. end: (): ReadonlyArray => {