From 19f0e8a083a60072e458e0c3a293714fdb03db49 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 27 Mar 2026 17:36:57 -0700 Subject: [PATCH] Patch ACP protocol to use request envelopes - Encode outgoing messages through ACP request envelopes - Add coverage for notification encoding failures --- packages/effect-acp/src/protocol.test.ts | 37 +++++++++++++++++++++--- packages/effect-acp/src/protocol.ts | 22 ++++---------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts index 10c1a8bd83..2772263460 100644 --- a/packages/effect-acp/src/protocol.test.ts +++ b/packages/effect-acp/src/protocol.test.ts @@ -1,3 +1,4 @@ +import * as AcpError from "./errors"; import * as Effect from "effect/Effect"; import * as Deferred from "effect/Deferred"; import * as Fiber from "effect/Fiber"; @@ -64,6 +65,8 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => { const outbound = yield* Queue.take(output); assert.deepEqual(decodeJson(outbound), { jsonrpc: "2.0", + id: "", + headers: [], method: "session/cancel", params: { sessionId: "session-1", @@ -133,23 +136,49 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => { direction: "outgoing", stage: "decoded", payload: { - jsonrpc: "2.0", - method: "session/cancel", - params: { + _tag: "Request", + id: "", + tag: "session/cancel", + payload: { sessionId: "session-1", }, + headers: [], }, }, { direction: "outgoing", stage: "raw", payload: - '{"jsonrpc":"2.0","method":"session/cancel","params":{"sessionId":"session-1"}}\n', + '{"jsonrpc":"2.0","method":"session/cancel","params":{"sessionId":"session-1"},"id":"","headers":[]}\n', }, ]); }), ); + it.effect("fails notification encoding through the declared ACP error channel", () => + Effect.gen(function* () { + const { stdio } = yield* makeInMemoryStdio(); + const transport = yield* AcpProtocol.makeAcpPatchedProtocol({ + stdio, + serverRequestMethods: new Set(), + }); + + const bigintError = yield* transport.notifications + .sendExtNotification("x/test", 1n) + .pipe(Effect.flip); + assert.instanceOf(bigintError, AcpError.AcpProtocolParseError); + assert.equal(bigintError.detail, "Failed to encode ACP message"); + + const circular: Record = {}; + circular.self = circular; + const circularError = yield* transport.notifications + .sendExtNotification("x/test", circular) + .pipe(Effect.flip); + assert.instanceOf(circularError, AcpError.AcpProtocolParseError); + assert.equal(circularError.detail, "Failed to encode ACP message"); + }), + ); + it.effect("supports generic extension requests over the patched transport", () => Effect.gen(function* () { const { stdio, input, output } = yield* makeInMemoryStdio(); diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts index 72b45c4def..35b297ef8f 100644 --- a/packages/effect-acp/src/protocol.ts +++ b/packages/effect-acp/src/protocol.ts @@ -449,23 +449,13 @@ export const makeAcpPatchedProtocol = ( method: string, payload: unknown, ) { - const message = { - jsonrpc: "2.0" as const, - method, - ...(payload !== undefined ? { params: payload } : {}), - }; - yield* logProtocol({ - direction: "outgoing", - stage: "decoded", - payload: message, + yield* offerOutgoing({ + _tag: "Request", + id: "", + tag: method, + payload, + headers: [], }); - const encoded = `${JSON.stringify(message)}\n`; - yield* logProtocol({ - direction: "outgoing", - stage: "raw", - payload: encoded, - }); - yield* Queue.offer(outgoing, encoded).pipe(Effect.asVoid); }); const sendRequest = Effect.fn("sendRequest")(function* (method: string, payload: unknown) {