Skip to content

Commit 20fddd2

Browse files
fix(effect-acp): a dying extension or notification handler no longer stops the reader
disableFatalDefects only covers core methods, which go through RpcServer. Extension requests and notifications are handled in the protocol layer, where Effect.ignore and matchEffect let a defect through and end the stdin reader: that request and every later one on the connection went unanswered, and termination never fired. A dying extension handler now answers its own request with an internal error, and notification handlers are guarded on their whole cause. The ACP tests now assert the defect itself, and the client test checks that a later request is still answered after each kind of handler dies. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f963d36 commit 20fddd2

5 files changed

Lines changed: 112 additions & 23 deletions

File tree

‎apps/server/src/observability/DefectReporter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ describe("DefectReporter", () => {
9494
yield* ErrorReporter.report(Cause.interrupt());
9595
yield* ErrorReporter.report(Cause.die(new Error("bug")));
9696

97-
assert.equal(errorMessage(yield* Queue.take(logs)), "bug");
98-
assert.equal(yield* Queue.size(logs), 0);
97+
// Reporters log synchronously, so the logs are already written.
98+
assert.deepEqual((yield* Queue.clear(logs)).map(errorMessage), ["bug"]);
9999
}).pipe(Effect.provide(DefectReporter.layer)),
100100
),
101101
);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ const SessionCancelNotification = jsonRpcNotification(
3535
const ExtPingNotification = jsonRpcNotification("x/ping", Schema.Struct({ count: Schema.Number }));
3636
const ExtRequest = jsonRpcRequest("x/test", Schema.Struct({ hello: Schema.String }));
3737
const ExtResponse = jsonRpcResponse(Schema.Struct({ ok: Schema.Boolean }));
38-
/** A JSON-RPC error response; only its id and the presence of an error matter here. */
39-
const ErrorResponse = Schema.Struct({ id: Schema.Number, error: Schema.Unknown });
38+
/** A response whose cause is a handler's defect, as RpcServer encodes it. */
39+
const DieResponse = Schema.Struct({
40+
id: Schema.Number,
41+
error: Schema.Struct({
42+
_tag: Schema.Literal("Cause"),
43+
data: Schema.Tuple([Schema.Struct({ _tag: Schema.Literal("Die") })]),
44+
}),
45+
});
4046
const decodeRequestPermissionRequest = Schema.decodeEffect(
4147
Schema.fromJsonString(RequestPermissionRequest),
4248
);
@@ -322,10 +328,9 @@ it.effect("effect-acp agent answers a request whose handler dies with an error f
322328
}),
323329
);
324330
const response = yield* Queue.take(output).pipe(
325-
Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(ErrorResponse))),
331+
Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(DieResponse))),
326332
);
327333
assert.equal(response.id, 7);
328-
assert.isNotNull(response.error);
329334
yield* Scope.close(scope, Exit.void);
330335
}),
331336
);

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

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ const ElicitationRequest = jsonRpcRequest("elicitation/create", AcpSchema.Create
5252
const ElicitationResponse = jsonRpcResponse(AcpSchema.CreateElicitationResponse);
5353
/** A JSON-RPC error response; only its id and the presence of an error matter here. */
5454
const ErrorResponse = Schema.Struct({ id: Schema.String, error: Schema.Unknown });
55+
/** A response whose cause is a handler's defect, as RpcServer encodes it. */
56+
const DieResponse = Schema.Struct({
57+
id: Schema.String,
58+
error: Schema.Struct({
59+
_tag: Schema.Literal("Cause"),
60+
data: Schema.Tuple([Schema.Struct({ _tag: Schema.Literal("Die") })]),
61+
}),
62+
});
5563
const decodePromptRequestLine = Schema.decodeEffect(Schema.fromJsonString(PromptRequest));
5664
const XAiPromptCompleteNotification = jsonRpcNotification(
5765
"_x.ai/session/prompt_complete",
@@ -1132,16 +1140,28 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
11321140
}),
11331141
);
11341142

1135-
it.effect("answers a request whose handler dies with an error for that request", () =>
1143+
it.effect("answers each request whose handler dies, and keeps reading", () =>
11361144
Effect.gen(function* () {
11371145
const { stdio, input, output } = yield* makeInMemoryStdio();
11381146
const scope = yield* Scope.make();
11391147
const acp = yield* AcpClient.make(stdio).pipe(Effect.provideService(Scope.Scope, scope));
1140-
yield* acp.handleRequestPermission(() => Effect.die(new Error("handler bug")));
1148+
const bug = () => Effect.die(new Error("handler bug"));
1149+
yield* acp.handleRequestPermission(bug);
1150+
yield* acp.handleExtRequest("x/dies", Schema.Unknown, bug);
1151+
yield* acp.handleSessionUpdate(bug);
1152+
const updates = yield* Queue.unbounded<string>();
1153+
yield* acp.handleSessionUpdate((notification) =>
1154+
Queue.offer(updates, notification.sessionId).pipe(Effect.asVoid),
1155+
);
1156+
yield* acp.handleExtRequest("x/test", Schema.Struct({ hello: Schema.String }), () =>
1157+
Effect.succeed({ ok: true }),
1158+
);
1159+
const send = <E>(line: Effect.Effect<Uint8Array, E>) =>
1160+
Effect.flatMap(line, (bytes) => Queue.offer(input, bytes));
1161+
const decodeDie = Schema.decodeEffect(Schema.fromJsonString(DieResponse));
11411162

1142-
yield* Queue.offer(
1143-
input,
1144-
yield* encodeJsonl(PermissionRequest, {
1163+
yield* send(
1164+
encodeJsonl(PermissionRequest, {
11451165
jsonrpc: "2.0",
11461166
id: "permission-a",
11471167
method: "session/request_permission",
@@ -1157,11 +1177,50 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
11571177
headers: [],
11581178
}),
11591179
);
1160-
const response = yield* Queue.take(output).pipe(
1180+
assert.equal((yield* Queue.take(output).pipe(Effect.flatMap(decodeDie))).id, "permission-a");
1181+
1182+
yield* send(
1183+
encodeJsonl(jsonRpcRequest("x/dies", Schema.Unknown), {
1184+
jsonrpc: "2.0",
1185+
id: "ext-a",
1186+
method: "x/dies",
1187+
params: {},
1188+
headers: [],
1189+
}),
1190+
);
1191+
const extDied = yield* Queue.take(output).pipe(
11611192
Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(ErrorResponse))),
11621193
);
1163-
assert.equal(response.id, "permission-a");
1164-
assert.isNotNull(response.error);
1194+
assert.equal(extDied.id, "ext-a");
1195+
1196+
yield* send(
1197+
encodeJsonl(SessionUpdateNotification, {
1198+
jsonrpc: "2.0",
1199+
method: "session/update",
1200+
params: {
1201+
sessionId: "session-1",
1202+
update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "hi" } },
1203+
},
1204+
}),
1205+
);
1206+
1207+
// The next session handler still ran.
1208+
assert.equal(yield* Queue.take(updates), "session-1");
1209+
1210+
// The reader survived all three: a later request is still answered.
1211+
yield* send(
1212+
encodeJsonl(ExtRequest, {
1213+
jsonrpc: "2.0",
1214+
id: "ext-b",
1215+
method: "x/test",
1216+
params: { hello: "world" },
1217+
headers: [],
1218+
}),
1219+
);
1220+
const answered = yield* Queue.take(output).pipe(
1221+
Effect.flatMap(Schema.decodeEffect(Schema.fromJsonString(ExtResponse))),
1222+
);
1223+
assert.deepEqual([answered.id, answered.result], ["ext-b", { ok: true }]);
11651224
yield* Scope.close(scope, Exit.void);
11661225
}),
11671226
);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,15 @@ export const make = Effect.fn("effect-acp/AcpClient.make")(function* (
838838
registration: BufferedNotificationHandler<A>,
839839
notification: A,
840840
) =>
841-
Effect.forEach(registration.handlers, (handler) => handler(notification).pipe(Effect.ignore), {
842-
discard: true,
843-
});
841+
// One handler failing or dying does not stop the others, or the reader.
842+
Effect.forEach(
843+
registration.handlers,
844+
(handler) =>
845+
handler(notification).pipe(
846+
Effect.ignoreCause({ log: true, message: "ACP notification handler failed" }),
847+
),
848+
{ discard: true },
849+
);
844850

845851
const flushBufferedNotifications = <A>(registration: BufferedNotificationHandler<A>) =>
846852
Effect.suspend(() => {

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Cause from "effect/Cause";
33
import * as Effect from "effect/Effect";
44
import * as Deferred from "effect/Deferred";
55
import * as Exit from "effect/Exit";
6+
import * as Option from "effect/Option";
67
import * as Queue from "effect/Queue";
78
import * as Ref from "effect/Ref";
89
import type * as PlatformError from "effect/PlatformError";
@@ -366,8 +367,12 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
366367
const dispatchNotification = (notification: AcpIncomingNotification) =>
367368
Queue.offer(notificationQueue, notification).pipe(
368369
Effect.andThen(
370+
// A failing or dying handler must not stop the reader, or every later
371+
// message on the connection goes unanswered.
369372
options.onNotification
370-
? options.onNotification(notification).pipe(Effect.ignore)
373+
? options
374+
.onNotification(notification)
375+
.pipe(Effect.ignoreCause({ log: true, message: "ACP notification handler failed" }))
371376
: Effect.void,
372377
),
373378
Effect.asVoid,
@@ -439,12 +444,26 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
439444
method: message.tag,
440445
})
441446
.pipe(
442-
Effect.matchEffect({
443-
onFailure: (error) =>
444-
respondWithError(
447+
Effect.matchCauseEffect({
448+
// A dying handler answers its own request, like a core handler, and
449+
// leaves the reader running.
450+
onFailure: (cause) => {
451+
const failure = Cause.findErrorOption(cause);
452+
return respondWithError(
445453
message.id,
446-
AcpError.AcpRequestError.fromExtensionHandlerError(error, message.tag),
447-
),
454+
Option.isSome(failure)
455+
? AcpError.AcpRequestError.fromExtensionHandlerError(failure.value, message.tag)
456+
: AcpError.AcpRequestError.internalError(
457+
`ACP extension request handler failed for method '${message.tag}'`,
458+
undefined,
459+
{
460+
method: message.tag,
461+
operation: "handle-extension-request",
462+
cause: Cause.squash(cause),
463+
},
464+
),
465+
);
466+
},
448467
onSuccess: (value) => respondWithSuccess(message.id, value),
449468
}),
450469
);

0 commit comments

Comments
 (0)