Skip to content

Commit 13c7de2

Browse files
only21milclaude
andauthored
fix(codex): approvals still appear after many stopped Codex turns (#23234)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0ce257b commit 13c7de2

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

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

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

421+
it.effect("releases a waiting request handler when Codex resolves its request", () =>
422+
Effect.gen(function* () {
423+
const { stdio, input, output } = yield* makeInMemoryStdio();
424+
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
425+
stdio,
426+
onRequest: (request) =>
427+
request.id === 33 ? Effect.succeed({ decision: "accept" }) : Effect.never,
428+
});
429+
430+
const messages = Array.from({ length: 32 }, (_, index) =>
431+
encodeUnknownJsonString({
432+
id: index + 1,
433+
method: "item/commandExecution/requestApproval",
434+
params: {},
435+
}),
436+
);
437+
for (let requestId = 1; requestId <= 32; requestId++) {
438+
messages.push(
439+
encodeUnknownJsonString({
440+
method: "serverRequest/resolved",
441+
params: { threadId: "thread-1", requestId },
442+
}),
443+
);
444+
}
445+
messages.push(
446+
encodeUnknownJsonString({
447+
id: 33,
448+
method: "item/commandExecution/requestApproval",
449+
params: {},
450+
}),
451+
);
452+
yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`));
453+
454+
assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
455+
id: 33,
456+
result: { decision: "accept" },
457+
});
458+
}),
459+
);
460+
461+
it.effect("keeps numeric and string request ids apart when Codex resolves one", () =>
462+
Effect.gen(function* () {
463+
const { stdio, input, output } = yield* makeInMemoryStdio();
464+
const interrupted: Array<string | number> = [];
465+
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
466+
stdio,
467+
onRequest: (request) =>
468+
request.id === 2
469+
? Effect.sync(() => ({ interrupted }))
470+
: Effect.never.pipe(
471+
Effect.onInterrupt(() => Effect.sync(() => interrupted.push(request.id))),
472+
),
473+
});
474+
475+
const messages = [
476+
{ id: 1, method: "item/commandExecution/requestApproval", params: {} },
477+
{ id: "1", method: "item/commandExecution/requestApproval", params: {} },
478+
{ method: "serverRequest/resolved", params: { threadId: "thread-1", requestId: "1" } },
479+
{ id: 2, method: "item/commandExecution/requestApproval", params: {} },
480+
].map((message) => encodeUnknownJsonString(message));
481+
yield* Queue.offer(input, encoder.encode(`${messages.join("\n")}\n`));
482+
483+
assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
484+
id: 2,
485+
result: { interrupted: ["1"] },
486+
});
487+
}),
488+
);
489+
421490
it.effect("interrupts pending request handlers when the protocol terminates", () =>
422491
Effect.gen(function* () {
423492
const { stdio, input } = yield* makeInMemoryStdio();

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

Lines changed: 15 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";
@@ -176,6 +177,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
176177
const terminationFailure = yield* Ref.make(Option.none<CodexError.CodexAppServerError>());
177178
const terminationSignal = yield* Deferred.make<void>();
178179
const activeRequestHandlers = yield* Ref.make(0);
180+
const requestHandlers = yield* FiberMap.make<string | number>().pipe(
181+
Scope.provide(requestHandlerScope),
182+
);
179183

180184
const logProtocol = (event: CodexAppServerProtocolLogEvent) => {
181185
if (event.direction === "incoming" && !options.logIncoming) {
@@ -343,7 +347,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
343347
Effect.asVoid,
344348
),
345349
),
346-
Effect.forkIn(requestHandlerScope, { startImmediately: true }),
350+
FiberMap.run(requestHandlers, request.id),
347351
Effect.asVoid,
348352
);
349353
}),
@@ -352,8 +356,17 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
352356
Effect.asVoid,
353357
);
354358

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

0 commit comments

Comments
 (0)