Skip to content

Commit 47d82f5

Browse files
only21milclaude
andcommitted
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 <[email protected]>
1 parent a8bb769 commit 47d82f5

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,46 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
417417
}),
418418
);
419419

420+
it.effect("releases a waiting request handler when Codex resolves its request", () =>
421+
Effect.gen(function* () {
422+
const { stdio, input, output } = yield* makeInMemoryStdio();
423+
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
424+
stdio,
425+
onRequest: (request) =>
426+
request.id === 33 ? Effect.succeed({ decision: "accept" }) : Effect.never,
427+
});
428+
429+
const messages = Array.from({ length: 32 }, (_, index) =>
430+
encodeUnknownJsonString({
431+
id: index + 1,
432+
method: "item/commandExecution/requestApproval",
433+
params: {},
434+
}),
435+
);
436+
for (let requestId = 1; requestId <= 32; requestId++) {
437+
messages.push(
438+
encodeUnknownJsonString({
439+
method: "serverRequest/resolved",
440+
params: { threadId: "thread-1", requestId },
441+
}),
442+
);
443+
}
444+
messages.push(
445+
encodeUnknownJsonString({
446+
id: 33,
447+
method: "item/commandExecution/requestApproval",
448+
params: {},
449+
}),
450+
);
451+
yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`));
452+
453+
assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
454+
id: 33,
455+
result: { decision: "accept" },
456+
});
457+
}),
458+
);
459+
420460
it.effect("interrupts pending request handlers when the protocol terminates", () =>
421461
Effect.gen(function* () {
422462
const { stdio, input } = yield* makeInMemoryStdio();

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Cause from "effect/Cause";
22
import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import * as Exit from "effect/Exit";
5+
import * as FiberMap from "effect/FiberMap";
56
import * as Option from "effect/Option";
67
import * as Queue from "effect/Queue";
78
import * as Ref from "effect/Ref";
@@ -175,6 +176,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
175176
const terminationFailure = yield* Ref.make(Option.none<CodexError.CodexAppServerError>());
176177
const terminationSignal = yield* Deferred.make<void>();
177178
const activeRequestHandlers = yield* Ref.make(0);
179+
const requestHandlers = yield* FiberMap.make<string>().pipe(Scope.provide(requestHandlerScope));
178180

179181
const logProtocol = (event: CodexAppServerProtocolLogEvent) => {
180182
if (event.direction === "incoming" && !options.logIncoming) {
@@ -342,7 +344,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
342344
Effect.asVoid,
343345
),
344346
),
345-
Effect.forkIn(requestHandlerScope, { startImmediately: true }),
347+
FiberMap.run(requestHandlers, String(request.id)),
346348
Effect.asVoid,
347349
);
348350
}),
@@ -351,8 +353,17 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
351353
Effect.asVoid,
352354
);
353355

356+
// Codex settles requests itself when their turn ends; a handler still waiting would hold its slot.
357+
const releaseResolvedRequest = (notification: CodexAppServerIncomingNotification) =>
358+
notification.method === "serverRequest/resolved" &&
359+
isObject(notification.params) &&
360+
isJsonRpcId(notification.params.requestId)
361+
? FiberMap.remove(requestHandlers, String(notification.params.requestId))
362+
: Effect.void;
363+
354364
const handleNotification = (notification: CodexAppServerIncomingNotification) =>
355-
Queue.offer(incomingNotifications, notification).pipe(
365+
releaseResolvedRequest(notification).pipe(
366+
Effect.andThen(Queue.offer(incomingNotifications, notification)),
356367
Effect.andThen(options.onNotification ? options.onNotification(notification) : Effect.void),
357368
Effect.asVoid,
358369
);

0 commit comments

Comments
 (0)