Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,38 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
}),
);

it.effect("reports the process exit when a write fails because the process exited", () =>
Effect.gen(function* () {
const { stdio: baseStdio } = yield* makeInMemoryStdio();
const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 });
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio: Stdio.make({
...baseStdio,
stdout: () =>
Sink.forEach(() =>
Effect.fail(
PlatformError.systemError({
_tag: "Unknown",
module: "Stdio",
method: "stdout",
cause: new Error("EPIPE"),
}),
),
),
}),
terminationError: Effect.succeed(exitError),
});

const failure = yield* transport.request("initialize", {}).pipe(
Effect.match({
onFailure: (error) => error,
onSuccess: () => assert.fail("Expected the exited process to fail the request"),
}),
);
assert.strictEqual(failure, exitError);
}),
);

it.effect("reports a process exit that follows input end", () =>
Effect.gen(function* () {
const { stdio, input } = yield* makeInMemoryStdio();
Expand Down
53 changes: 29 additions & 24 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
] as const;
}).pipe(Effect.flatten);

// Node ends stdout before it reports the exit of a crashed process, so wait briefly
// for the exit status. Pipes can also close while the process keeps running, so the
// wait must not hang pending requests.
const exitStatusOr = (
fallback: CodexError.CodexAppServerError,
operation: "read-input-stream" | "write-output-stream",
) =>
options.terminationError === undefined
? Effect.succeed(fallback)
: options.terminationError.pipe(
Effect.timeoutOrElse({
duration: EXIT_STATUS_GRACE,
orElse: () => Effect.succeed(fallback),
}),
// A defect here would leave termination marked handled but unfinished.
Effect.catchDefect((cause) =>
Effect.succeed(new CodexError.CodexAppServerTransportError({ operation, cause })),
),
);

const offerOutgoing =
/* Every protocol write reaches this helper; request spans cover the operation. */
Effect.fnUntraced(function* (message: Record<string, unknown>) {
Expand Down Expand Up @@ -474,29 +494,12 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
Effect.matchEffect({
onFailure: (error) => handleTermination(() => Effect.succeed(error)),
onSuccess: () =>
handleTermination(() => {
const ended = new CodexError.CodexAppServerInputStreamEndedError({});
// Node ends stdout before it reports the exit of a crashed process, so
// wait briefly for the exit status. stdout can also close while the
// process keeps running, so the wait must not hang pending requests.
return options.terminationError === undefined
? Effect.succeed(ended)
: options.terminationError.pipe(
Effect.timeoutOrElse({
duration: EXIT_STATUS_GRACE,
orElse: () => Effect.succeed(ended),
}),
// A defect here would leave termination marked handled but unfinished.
Effect.catchDefect((cause) =>
Effect.succeed(
new CodexError.CodexAppServerTransportError({
operation: "read-input-stream",
cause,
}),
),
),
);
}),
handleTermination(() =>
exitStatusOr(
new CodexError.CodexAppServerInputStreamEndedError({}),
"read-input-stream",
),
),
}),
),
}),
Expand All @@ -517,11 +520,13 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
Stream.run(options.stdio.stdout()),
Effect.catchCause((cause) =>
handleTermination(() =>
Effect.succeed(
// Writing to a process that already exited fails with EPIPE; report the exit.
exitStatusOr(
new CodexError.CodexAppServerTransportError({
operation: "write-output-stream",
cause: Cause.squash(cause),
}),
"write-output-stream",
),
),
),
Expand Down
Loading