The best way to control Claude Code, Codex and more from any computer or phone
fix(codex): an app-server that exits before initialize reports its exit, not EPIPE (#37029)
Julius Marminge committed
Nov 14, 2026 at 19:04 UTC
240fc6975b7b997dbd46b79d427899d58e976bc9
2 files changed
+61
-24
packages/effect-codex-app-server/src/protocol.test.ts
+32
@@ -992,6 +992,38 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
992
}),
993
);
994
995
+ it.effect("reports the process exit when a write fails because the process exited", () =>
996
+ Effect.gen(function* () {
997
+ const { stdio: baseStdio } = yield* makeInMemoryStdio();
998
+ const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 });
999
+ const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
1000
+ stdio: Stdio.make({
1001
+ ...baseStdio,
1002
+ stdout: () =>
1003
+ Sink.forEach(() =>
1004
+ Effect.fail(
1005
+ PlatformError.systemError({
1006
+ _tag: "Unknown",
1007
+ module: "Stdio",
1008
+ method: "stdout",
1009
+ cause: new Error("EPIPE"),
1010
+ }),
1011
+ ),
1012
+ ),
1013
+ }),
1014
+ terminationError: Effect.succeed(exitError),
1015
+ });
1016
+
1017
+ const failure = yield* transport.request("initialize", {}).pipe(
1018
+ Effect.match({
1019
+ onFailure: (error) => error,
1020
+ onSuccess: () => assert.fail("Expected the exited process to fail the request"),
1021
+ }),
1022
+ );
1023
+ assert.strictEqual(failure, exitError);
1024
+ }),
1025
+ );
1026
+
1027
it.effect("reports a process exit that follows input end", () =>
1028
Effect.gen(function* () {
1029
const { stdio, input } = yield* makeInMemoryStdio();
packages/effect-codex-app-server/src/protocol.ts
+29
-24
@@ -229,6 +229,26 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
229
] as const;
230
}).pipe(Effect.flatten);
231
232
+ // Node ends stdout before it reports the exit of a crashed process, so wait briefly
233
+ // for the exit status. Pipes can also close while the process keeps running, so the
234
+ // wait must not hang pending requests.
235
+ const exitStatusOr = (
236
+ fallback: CodexError.CodexAppServerError,
237
+ operation: "read-input-stream" | "write-output-stream",
238
+ ) =>
239
+ options.terminationError === undefined
240
+ ? Effect.succeed(fallback)
241
+ : options.terminationError.pipe(
242
+ Effect.timeoutOrElse({
243
+ duration: EXIT_STATUS_GRACE,
244
+ orElse: () => Effect.succeed(fallback),
245
+ }),
246
+ // A defect here would leave termination marked handled but unfinished.
247
+ Effect.catchDefect((cause) =>
248
+ Effect.succeed(new CodexError.CodexAppServerTransportError({ operation, cause })),
249
+ ),
250
+ );
251
+
252
const offerOutgoing =
253
/* Every protocol write reaches this helper; request spans cover the operation. */
254
Effect.fnUntraced(function* (message: Record<string, unknown>) {
@@ -474,29 +494,12 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
494
Effect.matchEffect({
495
onFailure: (error) => handleTermination(() => Effect.succeed(error)),
496
onSuccess: () =>
477
- handleTermination(() => {
478
- const ended = new CodexError.CodexAppServerInputStreamEndedError({});
479
- // Node ends stdout before it reports the exit of a crashed process, so
480
- // wait briefly for the exit status. stdout can also close while the
481
- // process keeps running, so the wait must not hang pending requests.
482
- return options.terminationError === undefined
483
- ? Effect.succeed(ended)
484
- : options.terminationError.pipe(
485
- Effect.timeoutOrElse({
486
- duration: EXIT_STATUS_GRACE,
487
- orElse: () => Effect.succeed(ended),
488
- }),
489
- // A defect here would leave termination marked handled but unfinished.
490
- Effect.catchDefect((cause) =>
491
- Effect.succeed(
492
- new CodexError.CodexAppServerTransportError({
493
- operation: "read-input-stream",
494
- cause,
495
- }),
496
- ),
497
- ),
498
- );
499
- }),
497
+ handleTermination(() =>
498
+ exitStatusOr(
499
+ new CodexError.CodexAppServerInputStreamEndedError({}),
500
+ "read-input-stream",
501
+ ),
502
+ ),
503
}),
504
),
505
}),
@@ -517,11 +520,13 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
520
Stream.run(options.stdio.stdout()),
521
Effect.catchCause((cause) =>
522
handleTermination(() =>
520
- Effect.succeed(
523
+ // Writing to a process that already exited fails with EPIPE; report the exit.
524
+ exitStatusOr(
525
new CodexError.CodexAppServerTransportError({
526
operation: "write-output-stream",
527
cause: Cause.squash(cause),
528
}),
529
+ "write-output-stream",
530
),
531
),
532
),