Skip to content
TypeScript

fix(acp): retain errors reported by idle work state (#21614)

Julius Marminge012e4ec0566c
diff --git a/packages/effect-acp/src/client.test.ts b/packages/effect-acp/src/client.test.ts
index bc10ec85719..e2bd0fdb818 100644
--- a/packages/effect-acp/src/client.test.ts
+++ b/packages/effect-acp/src/client.test.ts
@@ -268,6 +268,42 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
}),
);
+ it.effect("keeps the JSON-RPC error of a failed v2 idle", () =>
+ Effect.gen(function* () {
+ const { stdio, input } = yield* makeInMemoryStdio();
+ const delivered = yield* Deferred.make<AcpCompat.SessionNotification>();
+ const acp = yield* AcpClient.make(stdio);
+ yield* acp.handleSessionUpdate((notification) =>
+ Deferred.succeed(delivered, notification).pipe(Effect.asVoid),
+ );
+ const error = { code: -32000, message: "Please sign in to Claude", data: { retry: false } };
+ const idle = {
+ sessionUpdate: "state_update",
+ state: "idle",
+ stopReason: "error",
+ error,
+ } as const;
+
+ yield* Queue.offer(
+ input,
+ yield* encodeJsonl(SessionUpdateNotification, {
+ jsonrpc: "2.0",
+ method: "session/update",
+ params: {
+ sessionId: "session-1",
+ update: idle,
+ },
+ }),
+ );
+
+ const { update } = yield* Deferred.await(delivered);
+ assert.deepEqual(
+ update.sessionUpdate === "state_update" && update.state === "idle" ? update : undefined,
+ idle,
+ );
+ }),
+ );
+
it.effect("reports idle child termination and rejects later requests and notifications", () =>
Effect.gen(function* () {
const handle = yield* makeHandle();
diff --git a/packages/effect-acp/src/compat.ts b/packages/effect-acp/src/compat.ts
index f0454f68cf0..586a0b3048c 100644
--- a/packages/effect-acp/src/compat.ts
+++ b/packages/effect-acp/src/compat.ts
@@ -317,6 +317,7 @@ export type SessionUpdate =
readonly state: "idle";
readonly stopReason?: StopReason | null;
readonly usage?: Usage | null;
+ readonly error?: V2.Error | null;
readonly _meta?: Meta;
}
| {
diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts
index cc0a3ea7e3c..6a17c4d1201 100644
--- a/packages/effect-acp/src/protocol.test.ts
+++ b/packages/effect-acp/src/protocol.test.ts
@@ -16,7 +16,7 @@ import * as Ref from "effect/Ref";
import * as TestClock from "effect/testing/TestClock";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
-import { it, assert } from "@effect/vitest";
+import { it, assert, expect } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as AcpSchema from "./schema.ts";
@@ -283,18 +283,18 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
assert.equal(transformCalls, 1);
const decodeFailed = events.find(({ stage }) => stage === "decode_failed");
- assert.deepInclude(decodeFailed?.payload as object, {
+ expect(decodeFailed?.payload).toMatchObject({
operation: "decode-notification-payload",
method: "session/update",
- issueSummary: "update.used missing",
+ issueSummary: expect.stringContaining("update.used missing"),
});
assert.lengthOf(
events.filter(({ stage }) => stage === "decode_failed"),
2,
);
assert.lengthOf(warnings, 1);
- assert.deepInclude(warnings[0] as object, {
- issueSummary: "update.used missing",
+ expect(warnings[0]).toMatchObject({
+ issueSummary: expect.stringContaining("update.used missing"),
});
assert.notInclude(encodeUnknownJsonString({ decodeFailed, warnings }), secret);
}),
diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts
index 2f9b8b901a2..bc880884d75 100644
--- a/packages/effect-acp/src/protocol.ts
+++ b/packages/effect-acp/src/protocol.ts
@@ -84,7 +84,10 @@ export interface AcpPatchedProtocolOptions {
) => Effect.Effect<void, never>;
readonly transformSessionUpdate?: (
- notification: AcpSchema.UpdateSessionNotification | AcpSchemaV1.SessionNotification,
+ notification:
+ | AcpSchema.UpdateSessionNotification
+ | AcpSchemaV1.SessionNotification
+ | AcpCompat.SessionNotification,
) =>
| AcpSchema.UpdateSessionNotification
| AcpSchemaV1.SessionNotification
@@ -136,7 +139,22 @@ interface AcpOutgoingWriterState {
}
const decodeSessionUpdate = Schema.decodeUnknownEffect(
- Schema.Union([AcpSchema.UpdateSessionNotification, AcpSchemaV1.SessionNotification]),
+ Schema.Union([
+ // Claude ACP v2 reports prompt failures on idle; alpha.3 omits that field.
+ Schema.Struct({
+ ...AcpSchema.UpdateSessionNotification.fields,
+ update: Schema.Struct({
+ sessionUpdate: Schema.Literal("state_update"),
+ state: Schema.Literal("idle"),
+ stopReason: Schema.optionalKey(Schema.NullOr(AcpSchema.StopReason)),
+ usage: Schema.optionalKey(Schema.NullOr(AcpSchema.Usage)),
+ error: Schema.optionalKey(Schema.NullOr(AcpSchema.Error)),
+ _meta: AcpSchema.UpdateSessionNotification.fields._meta,
+ }),
+ }),
+ AcpSchema.UpdateSessionNotification,
+ AcpSchemaV1.SessionNotification,
+ ]),
);
const decodeElicitationComplete = Schema.decodeUnknownEffect(
AcpSchema.CompleteElicitationNotification,