Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/effect-acp/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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";
Expand Down
4 changes: 2 additions & 2 deletions packages/effect-acp/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<typeof codec.encode>[0]) => {
const encoded = codec.encode(response);
Expand Down
Loading