Skip to content

Commit fbe670c

Browse files
authored
fix(acp): request ids stay within signed 32-bit so Kotlin agents answer (pingdotgg#15374)
1 parent 09e54fd commit fbe670c

5 files changed

Lines changed: 123 additions & 4 deletions

File tree

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

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ it.effect("effect-acp agent handles core agent requests and outbound client requ
225225
}),
226226
);
227227

228-
it.effect("effect-acp agent uses distinct ids for RPC calls and extension requests", () =>
228+
it.effect("effect-acp agent keeps RPC ids within int32 and apart from extension ids", () =>
229229
Effect.gen(function* () {
230230
const { stdio, input, output } = yield* makeInMemoryStdio();
231231
const scope = yield* Scope.make();
@@ -274,6 +274,11 @@ it.effect("effect-acp agent uses distinct ids for RPC calls and extension reques
274274
: yield* decodedExt(firstOutbound);
275275

276276
assert.notEqual(permissionRequest.id, extRequest.id);
277+
// Keep locally generated ids compatible with SDKs that decode signed int32.
278+
assert.typeOf(permissionRequest.id, "number");
279+
assert.equal(permissionRequest.id, 2 ** 30);
280+
assert.isAtMost(Number(permissionRequest.id), 2 ** 31 - 1);
281+
assert.equal(extRequest.id, 1);
277282

278283
yield* Queue.offer(
279284
input,
@@ -300,10 +305,95 @@ it.effect("effect-acp agent uses distinct ids for RPC calls and extension reques
300305
const permission = yield* Fiber.join(permissionFiber);
301306
assert.equal(permission.outcome.outcome, "selected");
302307
assert.deepEqual(yield* Fiber.join(extFiber), { ok: true });
308+
309+
const nextPermissionFiber = yield* agent.client
310+
.requestPermission(permissionRequest.params)
311+
.pipe(Effect.forkScoped);
312+
const nextPermissionRequest = yield* decodedPermission(yield* Queue.take(output));
313+
assert.equal(nextPermissionRequest.id, 2 ** 30 + 1);
314+
yield* Queue.offer(
315+
input,
316+
yield* encodeJsonl(RequestPermissionResponse, {
317+
jsonrpc: "2.0",
318+
id: nextPermissionRequest.id,
319+
result: { outcome: { outcome: "cancelled" } },
320+
}),
321+
);
322+
assert.equal((yield* Fiber.join(nextPermissionFiber)).outcome.outcome, "cancelled");
303323
}).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
304324
}),
305325
);
306326

327+
it.effect.each([0, 2 ** 30, 2 ** 32])(
328+
"effect-acp agent answers incoming id %s independently of outbound ids",
329+
(requestId) =>
330+
Effect.gen(function* () {
331+
const { stdio, input, output } = yield* makeInMemoryStdio();
332+
const agent = yield* AcpAgent.make(stdio);
333+
const contexts = yield* Ref.make<Array<AcpProtocol.AcpRequestContext>>([]);
334+
yield* agent.handleInitialize((_request, context) =>
335+
Ref.update(contexts, (current) => [...current, context]).pipe(
336+
Effect.as({
337+
protocolVersion: 2,
338+
capabilities: {},
339+
info: { name: "mock-agent", version: "0.0.0" },
340+
}),
341+
),
342+
);
343+
344+
// Opposite directions may reuse an id even while a request is pending.
345+
const outboundFiber = yield* agent.client
346+
.requestPermission({
347+
sessionId: "session-1",
348+
title: "Allow mock action",
349+
subject: {
350+
type: "tool_call",
351+
toolCall: { toolCallId: "tool-1", title: "Allow mock action" },
352+
},
353+
options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
354+
})
355+
.pipe(Effect.forkScoped);
356+
const outbound = yield* decodeRequestPermissionRequest(yield* Queue.take(output));
357+
358+
yield* Queue.offer(
359+
input,
360+
yield* encodeJsonl(InitializeRequest, {
361+
jsonrpc: "2.0",
362+
id: requestId,
363+
method: "initialize",
364+
params: {
365+
protocolVersion: 2,
366+
capabilities: {},
367+
info: { name: "test-client", version: "0.0.0" },
368+
},
369+
headers: [],
370+
}),
371+
);
372+
assert.deepEqual(yield* decodeInitializeResponse(yield* Queue.take(output)), {
373+
jsonrpc: "2.0",
374+
id: requestId,
375+
result: {
376+
protocolVersion: 2,
377+
capabilities: {},
378+
info: { name: "mock-agent", version: "0.0.0" },
379+
},
380+
});
381+
assert.deepEqual(yield* Ref.get(contexts), [
382+
{ requestId: `$t3:jsonrpc:number:${requestId}`, method: "initialize" },
383+
]);
384+
385+
yield* Queue.offer(
386+
input,
387+
yield* encodeJsonl(RequestPermissionResponse, {
388+
jsonrpc: "2.0",
389+
id: outbound.id,
390+
result: { outcome: { outcome: "cancelled" } },
391+
}),
392+
);
393+
assert.equal((yield* Fiber.join(outboundFiber)).outcome.outcome, "cancelled");
394+
}),
395+
);
396+
307397
it.effect("effect-acp agent answers a request whose handler dies with an error for it", () =>
308398
Effect.gen(function* () {
309399
const { stdio, input, output } = yield* makeInMemoryStdio();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export const make = Effect.fn("effect-acp/AcpAgent.make")(function* (
387387
Effect.forkScoped,
388388
);
389389

390-
let nextRpcRequestId = 2 ** 32;
390+
let nextRpcRequestId = AcpProtocol.RPC_REQUEST_ID_START;
391391
const rpc = yield* RpcClient.make(AcpRpcs.ClientRpcs, {
392392
generateRequestId: () => RpcMessage.RequestId(nextRpcRequestId++),
393393
}).pipe(Effect.provideService(RpcClient.Protocol, transport.clientProtocol));

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
753753
}),
754754
);
755755

756-
it.effect("uses distinct ids for RPC calls and extension requests", () =>
756+
it.effect("keeps RPC ids within int32 and apart from extension ids", () =>
757757
Effect.gen(function* () {
758758
const { stdio, input, output } = yield* makeInMemoryStdio();
759759
const scope = yield* Scope.make();
@@ -794,6 +794,11 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
794794
: yield* decodedExt(firstOutbound);
795795

796796
assert.notEqual(initializeRequest.id, extRequest.id);
797+
// Keep locally generated ids compatible with SDKs that decode signed int32.
798+
assert.typeOf(initializeRequest.id, "number");
799+
assert.equal(initializeRequest.id, 2 ** 30);
800+
assert.isAtMost(Number(initializeRequest.id), 2 ** 31 - 1);
801+
assert.equal(extRequest.id, 1);
797802

798803
yield* Queue.offer(
799804
input,
@@ -821,6 +826,23 @@ it.layer(NodeServices.layer)("effect-acp client", (it) => {
821826

822827
yield* Fiber.join(initializeFiber);
823828
assert.deepEqual(yield* Fiber.join(extFiber), { ok: true });
829+
830+
const sessionFiber = yield* acp.agent
831+
.createSession({ cwd: "/tmp", mcpServers: [] })
832+
.pipe(Effect.forkScoped);
833+
const sessionRequest = yield* Schema.decodeEffect(
834+
Schema.fromJsonString(jsonRpcRequest("session/new", AcpSchema.NewSessionRequest)),
835+
)(yield* Queue.take(output));
836+
assert.equal(sessionRequest.id, 2 ** 30 + 1);
837+
yield* Queue.offer(
838+
input,
839+
yield* encodeJsonl(jsonRpcResponse(AcpSchema.NewSessionResponse), {
840+
jsonrpc: "2.0",
841+
id: sessionRequest.id,
842+
result: { sessionId: "session-1" },
843+
}),
844+
);
845+
assert.equal((yield* Fiber.join(sessionFiber)).sessionId, "session-1");
824846
yield* Scope.close(scope, Exit.void);
825847
}),
826848
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ export const make = Effect.fn("effect-acp/AcpClient.make")(function* (
10301030
Effect.forkScoped,
10311031
);
10321032

1033-
let nextRpcRequestId = 2 ** 32;
1033+
let nextRpcRequestId = AcpProtocol.RPC_REQUEST_ID_START;
10341034
const rpc = yield* RpcClient.make(AcpRpcs.CompatAgentRpcs, {
10351035
generateRequestId: () => RpcMessage.RequestId(nextRpcRequestId++),
10361036
}).pipe(Effect.provideService(RpcClient.Protocol, transport.clientProtocol));

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ const encodeJsonRpcNotification = Schema.encodeUnknownExit(
182182
),
183183
);
184184

185+
/**
186+
* First id for typed Effect RPC requests. Starts within signed int32 for SDKs that reject
187+
* larger numeric ids (e.g. the Kotlin ACP SDK decodes ids as `Int`), while staying far
188+
* above extension ids, which count up from 1.
189+
*/
190+
export const RPC_REQUEST_ID_START = 2 ** 30;
191+
185192
const isEffectRpcRequestId = (requestId: AcpError.AcpRequestId): boolean =>
186193
typeof requestId === "number" && Number.isSafeInteger(requestId);
187194

0 commit comments

Comments
 (0)