Skip to content

Commit 82f1d04

Browse files
only21milclaude
andcommitted
fix(codex): keep numeric and string request ids apart in handler map
JSON-RPC ids 1 and "1" are distinct, but the handler FiberMap keyed both as "1". A second request replaced and interrupted the first without a response, and serverRequest/resolved could release the wrong handler. Key the map by the raw id instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 47d82f5 commit 82f1d04

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎packages/effect-codex-app-server/src/protocol.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,35 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
457457
}),
458458
);
459459

460+
it.effect("keeps numeric and string request ids apart when Codex resolves one", () =>
461+
Effect.gen(function* () {
462+
const { stdio, input, output } = yield* makeInMemoryStdio();
463+
const interrupted: Array<string | number> = [];
464+
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
465+
stdio,
466+
onRequest: (request) =>
467+
request.id === 2
468+
? Effect.sync(() => ({ interrupted }))
469+
: Effect.never.pipe(
470+
Effect.onInterrupt(() => Effect.sync(() => interrupted.push(request.id))),
471+
),
472+
});
473+
474+
const messages = [
475+
{ id: 1, method: "item/commandExecution/requestApproval", params: {} },
476+
{ id: "1", method: "item/commandExecution/requestApproval", params: {} },
477+
{ method: "serverRequest/resolved", params: { threadId: "thread-1", requestId: "1" } },
478+
{ id: 2, method: "item/commandExecution/requestApproval", params: {} },
479+
].map((message) => encodeUnknownJsonString(message));
480+
yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`));
481+
482+
assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
483+
id: 2,
484+
result: { interrupted: ["1"] },
485+
});
486+
}),
487+
);
488+
460489
it.effect("interrupts pending request handlers when the protocol terminates", () =>
461490
Effect.gen(function* () {
462491
const { stdio, input } = yield* makeInMemoryStdio();

‎packages/effect-codex-app-server/src/protocol.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
176176
const terminationFailure = yield* Ref.make(Option.none<CodexError.CodexAppServerError>());
177177
const terminationSignal = yield* Deferred.make<void>();
178178
const activeRequestHandlers = yield* Ref.make(0);
179-
const requestHandlers = yield* FiberMap.make<string>().pipe(Scope.provide(requestHandlerScope));
179+
const requestHandlers = yield* FiberMap.make<string | number>().pipe(
180+
Scope.provide(requestHandlerScope),
181+
);
180182

181183
const logProtocol = (event: CodexAppServerProtocolLogEvent) => {
182184
if (event.direction === "incoming" && !options.logIncoming) {
@@ -344,7 +346,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
344346
Effect.asVoid,
345347
),
346348
),
347-
FiberMap.run(requestHandlers, String(request.id)),
349+
FiberMap.run(requestHandlers, request.id),
348350
Effect.asVoid,
349351
);
350352
}),
@@ -358,7 +360,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
358360
notification.method === "serverRequest/resolved" &&
359361
isObject(notification.params) &&
360362
isJsonRpcId(notification.params.requestId)
361-
? FiberMap.remove(requestHandlers, String(notification.params.requestId))
363+
? FiberMap.remove(requestHandlers, notification.params.requestId)
362364
: Effect.void;
363365

364366
const handleNotification = (notification: CodexAppServerIncomingNotification) =>

0 commit comments

Comments
 (0)