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
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe("CodexAdapterV2 runtime policy", () => {
});

describe("CodexAdapterV2 process spawning", () => {
it.effect("reports the app-server process exiting to the factory's caller", () =>
it.live("reports the app-server process exiting to the factory's caller", () =>
Effect.gen(function* () {
const factory = yield* CodexAdapterV2.CodexAppServerClientFactory.pipe(
Effect.provide(
Expand Down
22 changes: 22 additions & 0 deletions packages/effect-codex-app-server/src/_internal/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ describe("Codex App Server child process termination", () => {
}),
);

it.effect("waits for stderr to drain when the exit status arrives first", () =>
Effect.gen(function* () {
const exitObserved = yield* Deferred.make<void>();
const stderr = Stream.fromEffect(Deferred.await(exitObserved)).pipe(
Stream.as(new TextEncoder().encode("Error: invalid config\n")),
);
const tail = yield* makeStderrTail(stderr);
const error = yield* makeTerminationError(
{
pid: ChildProcessSpawner.ProcessId(56),
exitCode: Deferred.succeed(exitObserved, undefined).pipe(
Effect.as(ChildProcessSpawner.ExitCode(1)),
),
},
tail.read,
);

assert.instanceOf(error, CodexError.CodexAppServerProcessExitedError);
assert.equal(error.stderr, "Error: invalid config");
}),
);

it.effect("retains the process identifier and exact exit-status cause", () =>
Effect.gen(function* () {
const rootCause = new Error("private process diagnostics");
Expand Down
9 changes: 7 additions & 2 deletions packages/effect-codex-app-server/src/_internal/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ export const makeStderrTail = Effect.fn("makeStderrTail")(function* (
Effect.ignore,
Effect.forkScoped,
);
// One bounded wait shared by every caller, so a later read never adds a second delay.
const awaitDrained = yield* Effect.cached(
Fiber.join(drain).pipe(Effect.timeout("250 millis"), Effect.ignore),
);
return {
read: Ref.get(tail),
awaitDrained: Fiber.join(drain).pipe(Effect.timeout("250 millis"), Effect.ignore),
// The exit status can arrive before stderr finishes, so a read waits for the drain.
read: Effect.andThen(awaitDrained, Ref.get(tail)),
awaitDrained,
};
});

Expand Down
38 changes: 38 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,42 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
assert.strictEqual(yield* Deferred.await(termination), exitError);
}),
);

it.effect("reports a process exit that follows a failed write", () =>
Effect.gen(function* () {
const { stdio: baseStdio } = yield* makeInMemoryStdio();
const writeFailure = PlatformError.systemError({
_tag: "Unknown",
module: "Stdio",
method: "stdout",
cause: new Error("EPIPE"),
});
const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 });
const exited = yield* Deferred.make<CodexError.CodexAppServerError>();
const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio: Stdio.make({
args: baseStdio.args,
stdin: baseStdio.stdin,
stdout: () => Sink.forEach(() => Effect.fail(writeFailure)),
stderr: baseStdio.stderr,
}),
terminationError: Deferred.await(exited),
onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
});

const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
yield* TestClock.adjust("100 millis");
yield* Deferred.succeed(exited, exitError);

assert.strictEqual(yield* Deferred.await(termination), exitError);
const requestFailure = yield* Fiber.join(pending).pipe(
Effect.match({
onFailure: (error) => error,
onSuccess: () => assert.fail("Expected the pending request to fail"),
}),
);
assert.strictEqual(requestFailure, exitError);
}),
);
});
31 changes: 16 additions & 15 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
] as const;
}).pipe(Effect.flatten);

// A dying process breaks its pipes before Node reports its exit, so wait briefly for the
// exit status. A pipe can also break while the process keeps running, so the wait is bounded.
const exitStatusOr = (fallback: CodexError.CodexAppServerError) =>
options.terminationError === undefined
? Effect.succeed(fallback)
: options.terminationError.pipe(
Effect.timeoutOrElse({
duration: EXIT_STATUS_GRACE,
orElse: () => Effect.succeed(fallback),
}),
);

const offerOutgoing =
/* Every protocol write reaches this helper; request spans cover the operation. */
Effect.fnUntraced(function* (message: Record<string, unknown>) {
Expand Down Expand Up @@ -459,20 +471,9 @@ 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),
}),
);
}),
handleTermination(() =>
exitStatusOr(new CodexError.CodexAppServerInputStreamEndedError({})),
),
}),
),
}),
Expand All @@ -483,7 +484,7 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
Stream.run(options.stdio.stdout()),
Effect.catch((cause) =>
handleTermination(() =>
Effect.succeed(
exitStatusOr(
Comment thread
Andrey170170 marked this conversation as resolved.
Resolved
new CodexError.CodexAppServerTransportError({
operation: "write-output-stream",
cause,
Expand Down