Skip to content

Commit 012e4ec

Browse files
fix(acp): retain errors reported by idle work state (#21614)
1 parent 3eaebac commit 012e4ec

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,42 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
268268
}),
269269
);
270270

271+
it.effect("keeps the JSON-RPC error of a failed v2 idle", () =>
272+
Effect.gen(function* () {
273+
const { stdio, input } = yield* makeInMemoryStdio();
274+
const delivered = yield* Deferred.make<AcpCompat.SessionNotification>();
275+
const acp = yield* AcpClient.make(stdio);
276+
yield* acp.handleSessionUpdate((notification) =>
277+
Deferred.succeed(delivered, notification).pipe(Effect.asVoid),
278+
);
279+
const error = { code: -32000, message: "Please sign in to Claude", data: { retry: false } };
280+
const idle = {
281+
sessionUpdate: "state_update",
282+
state: "idle",
283+
stopReason: "error",
284+
error,
285+
} as const;
286+
287+
yield* Queue.offer(
288+
input,
289+
yield* encodeJsonl(SessionUpdateNotification, {
290+
jsonrpc: "2.0",
291+
method: "session/update",
292+
params: {
293+
sessionId: "session-1",
294+
update: idle,
295+
},
296+
}),
297+
);
298+
299+
const { update } = yield* Deferred.await(delivered);
300+
assert.deepEqual(
301+
update.sessionUpdate === "state_update" && update.state === "idle" ? update : undefined,
302+
idle,
303+
);
304+
}),
305+
);
306+
271307
it.effect("reports idle child termination and rejects later requests and notifications", () =>
272308
Effect.gen(function* () {
273309
const handle = yield* makeHandle();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ export type SessionUpdate =
317317
readonly state: "idle";
318318
readonly stopReason?: StopReason | null;
319319
readonly usage?: Usage | null;
320+
readonly error?: V2.Error | null;
320321
readonly _meta?: Meta;
321322
}
322323
| {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as Ref from "effect/Ref";
1616
import * as TestClock from "effect/testing/TestClock";
1717
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1818

19-
import { it, assert } from "@effect/vitest";
19+
import { it, assert, expect } from "@effect/vitest";
2020
import * as NodeServices from "@effect/platform-node/NodeServices";
2121

2222
import * as AcpSchema from "./schema.ts";
@@ -283,18 +283,18 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
283283
assert.equal(transformCalls, 1);
284284

285285
const decodeFailed = events.find(({ stage }) => stage === "decode_failed");
286-
assert.deepInclude(decodeFailed?.payload as object, {
286+
expect(decodeFailed?.payload).toMatchObject({
287287
operation: "decode-notification-payload",
288288
method: "session/update",
289-
issueSummary: "update.used missing",
289+
issueSummary: expect.stringContaining("update.used missing"),
290290
});
291291
assert.lengthOf(
292292
events.filter(({ stage }) => stage === "decode_failed"),
293293
2,
294294
);
295295
assert.lengthOf(warnings, 1);
296-
assert.deepInclude(warnings[0] as object, {
297-
issueSummary: "update.used missing",
296+
expect(warnings[0]).toMatchObject({
297+
issueSummary: expect.stringContaining("update.used missing"),
298298
});
299299
assert.notInclude(encodeUnknownJsonString({ decodeFailed, warnings }), secret);
300300
}),

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export interface AcpPatchedProtocolOptions {
8484
) => Effect.Effect<void, never>;
8585

8686
readonly transformSessionUpdate?: (
87-
notification: AcpSchema.UpdateSessionNotification | AcpSchemaV1.SessionNotification,
87+
notification:
88+
| AcpSchema.UpdateSessionNotification
89+
| AcpSchemaV1.SessionNotification
90+
| AcpCompat.SessionNotification,
8891
) =>
8992
| AcpSchema.UpdateSessionNotification
9093
| AcpSchemaV1.SessionNotification
@@ -136,7 +139,22 @@ interface AcpOutgoingWriterState {
136139
}
137140

138141
const decodeSessionUpdate = Schema.decodeUnknownEffect(
139-
Schema.Union([AcpSchema.UpdateSessionNotification, AcpSchemaV1.SessionNotification]),
142+
Schema.Union([
143+
// Claude ACP v2 reports prompt failures on idle; alpha.3 omits that field.
144+
Schema.Struct({
145+
...AcpSchema.UpdateSessionNotification.fields,
146+
update: Schema.Struct({
147+
sessionUpdate: Schema.Literal("state_update"),
148+
state: Schema.Literal("idle"),
149+
stopReason: Schema.optionalKey(Schema.NullOr(AcpSchema.StopReason)),
150+
usage: Schema.optionalKey(Schema.NullOr(AcpSchema.Usage)),
151+
error: Schema.optionalKey(Schema.NullOr(AcpSchema.Error)),
152+
_meta: AcpSchema.UpdateSessionNotification.fields._meta,
153+
}),
154+
}),
155+
AcpSchema.UpdateSessionNotification,
156+
AcpSchemaV1.SessionNotification,
157+
]),
140158
);
141159
const decodeElicitationComplete = Schema.decodeUnknownEffect(
142160
AcpSchema.CompleteElicitationNotification,

0 commit comments

Comments
 (0)