From 47d82f5fa95f7ace792f445cece976a1dd89b3f8 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Wed, 21 Oct 2026 18:49:03 -0500 Subject: [PATCH 1/2] fix(codex): release an approval handler when Codex resolves its request Codex settles a pending server request itself when its turn ends, for example after Stop, and sends serverRequest/resolved. The transport ignored it, so the request handler kept waiting and kept one of its 32 slots. After 32 stopped approvals, every later approval or question was refused with "Too many Codex requests are already active." Track request handlers by request id and interrupt the matching handler when serverRequest/resolved arrives. No reply is sent, since Codex has already settled the request. Co-Authored-By: Claude Opus 5.5 --- .../src/protocol.test.ts | 40 +++++++++++++++++++ .../effect-codex-app-server/src/protocol.ts | 15 ++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index 10e979c650108..92016ccb16a41 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -417,6 +417,46 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { }), ); + it.effect("releases a waiting request handler when Codex resolves its request", () => + Effect.gen(function* () { + const { stdio, input, output } = yield* makeInMemoryStdio(); + yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + onRequest: (request) => + request.id === 33 ? Effect.succeed({ decision: "accept" }) : Effect.never, + }); + + const messages = Array.from({ length: 32 }, (_, index) => + encodeUnknownJsonString({ + id: index + 1, + method: "item/commandExecution/requestApproval", + params: {}, + }), + ); + for (let requestId = 1; requestId <= 32; requestId++) { + messages.push( + encodeUnknownJsonString({ + method: "serverRequest/resolved", + params: { threadId: "thread-1", requestId }, + }), + ); + } + messages.push( + encodeUnknownJsonString({ + id: 33, + method: "item/commandExecution/requestApproval", + params: {}, + }), + ); + yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`)); + + assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), { + id: 33, + result: { decision: "accept" }, + }); + }), + ); + it.effect("interrupts pending request handlers when the protocol terminates", () => Effect.gen(function* () { const { stdio, input } = yield* makeInMemoryStdio(); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index bf67174033f4d..0403128eee3cf 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -2,6 +2,7 @@ import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as FiberMap from "effect/FiberMap"; import * as Option from "effect/Option"; import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; @@ -175,6 +176,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa const terminationFailure = yield* Ref.make(Option.none()); const terminationSignal = yield* Deferred.make(); const activeRequestHandlers = yield* Ref.make(0); + const requestHandlers = yield* FiberMap.make().pipe(Scope.provide(requestHandlerScope)); const logProtocol = (event: CodexAppServerProtocolLogEvent) => { if (event.direction === "incoming" && !options.logIncoming) { @@ -342,7 +344,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.asVoid, ), ), - Effect.forkIn(requestHandlerScope, { startImmediately: true }), + FiberMap.run(requestHandlers, String(request.id)), Effect.asVoid, ); }), @@ -351,8 +353,17 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.asVoid, ); + // Codex settles requests itself when their turn ends; a handler still waiting would hold its slot. + const releaseResolvedRequest = (notification: CodexAppServerIncomingNotification) => + notification.method === "serverRequest/resolved" && + isObject(notification.params) && + isJsonRpcId(notification.params.requestId) + ? FiberMap.remove(requestHandlers, String(notification.params.requestId)) + : Effect.void; + const handleNotification = (notification: CodexAppServerIncomingNotification) => - Queue.offer(incomingNotifications, notification).pipe( + releaseResolvedRequest(notification).pipe( + Effect.andThen(Queue.offer(incomingNotifications, notification)), Effect.andThen(options.onNotification ? options.onNotification(notification) : Effect.void), Effect.asVoid, ); From 82f1d04fa3fbcff0f3190bc89f7120e7b7fcadf4 Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Fri, 23 Oct 2026 22:10:37 -0500 Subject: [PATCH 2/2] 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 --- .../src/protocol.test.ts | 29 +++++++++++++++++++ .../effect-codex-app-server/src/protocol.ts | 8 +++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/effect-codex-app-server/src/protocol.test.ts b/packages/effect-codex-app-server/src/protocol.test.ts index 92016ccb16a41..dcb6da696f799 100644 --- a/packages/effect-codex-app-server/src/protocol.test.ts +++ b/packages/effect-codex-app-server/src/protocol.test.ts @@ -457,6 +457,35 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => { }), ); + it.effect("keeps numeric and string request ids apart when Codex resolves one", () => + Effect.gen(function* () { + const { stdio, input, output } = yield* makeInMemoryStdio(); + const interrupted: Array = []; + yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ + stdio, + onRequest: (request) => + request.id === 2 + ? Effect.sync(() => ({ interrupted })) + : Effect.never.pipe( + Effect.onInterrupt(() => Effect.sync(() => interrupted.push(request.id))), + ), + }); + + const messages = [ + { id: 1, method: "item/commandExecution/requestApproval", params: {} }, + { id: "1", method: "item/commandExecution/requestApproval", params: {} }, + { method: "serverRequest/resolved", params: { threadId: "thread-1", requestId: "1" } }, + { id: 2, method: "item/commandExecution/requestApproval", params: {} }, + ].map((message) => encodeUnknownJsonString(message)); + yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`)); + + assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), { + id: 2, + result: { interrupted: ["1"] }, + }); + }), + ); + it.effect("interrupts pending request handlers when the protocol terminates", () => Effect.gen(function* () { const { stdio, input } = yield* makeInMemoryStdio(); diff --git a/packages/effect-codex-app-server/src/protocol.ts b/packages/effect-codex-app-server/src/protocol.ts index 0403128eee3cf..33abd2bc14b04 100644 --- a/packages/effect-codex-app-server/src/protocol.ts +++ b/packages/effect-codex-app-server/src/protocol.ts @@ -176,7 +176,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa const terminationFailure = yield* Ref.make(Option.none()); const terminationSignal = yield* Deferred.make(); const activeRequestHandlers = yield* Ref.make(0); - const requestHandlers = yield* FiberMap.make().pipe(Scope.provide(requestHandlerScope)); + const requestHandlers = yield* FiberMap.make().pipe( + Scope.provide(requestHandlerScope), + ); const logProtocol = (event: CodexAppServerProtocolLogEvent) => { if (event.direction === "incoming" && !options.logIncoming) { @@ -344,7 +346,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa Effect.asVoid, ), ), - FiberMap.run(requestHandlers, String(request.id)), + FiberMap.run(requestHandlers, request.id), Effect.asVoid, ); }), @@ -358,7 +360,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa notification.method === "serverRequest/resolved" && isObject(notification.params) && isJsonRpcId(notification.params.requestId) - ? FiberMap.remove(requestHandlers, String(notification.params.requestId)) + ? FiberMap.remove(requestHandlers, notification.params.requestId) : Effect.void; const handleNotification = (notification: CodexAppServerIncomingNotification) =>