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
46 changes: 46 additions & 0 deletions packages/effect-codex-app-server/src/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Queue from "effect/Queue";
import * as Schema from "effect/Schema";
import * as Stdio from "effect/Stdio";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";

import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
Expand Down Expand Up @@ -644,4 +645,49 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
assert.notInclude(error.message, secret);
}),
);

it.effect("fails pending requests when input ends while the process is still running", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio,
terminationError: Effect.never,
});
const pending = yield* transport.request("thread/read", {}).pipe(Effect.forkScoped);
yield* Queue.take(output);

yield* Queue.end(input);
yield* TestClock.adjust("249 millis");
assert.isUndefined(pending.pollUnsafe());
yield* TestClock.adjust("1 millis");

const failure = yield* Fiber.join(pending).pipe(
Effect.match({
onFailure: (error) => error,
onSuccess: () => assert.fail("Expected the pending request to fail"),
}),
);
assert.instanceOf(failure, CodexError.CodexAppServerInputStreamEndedError);
}),
);

it.effect("reports a process exit that follows input end", () =>
Effect.gen(function* () {
const { stdio, input } = yield* makeInMemoryStdio();
const exitError = new CodexError.CodexAppServerProcessExitedError({ code: 1, pid: 123 });
const exited = yield* Deferred.make<CodexError.CodexAppServerError>();
const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
stdio,
terminationError: Deferred.await(exited),
onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
});

yield* Queue.end(input);
yield* TestClock.adjust("100 millis");
yield* Deferred.succeed(exited, exitError);

assert.strictEqual(yield* Deferred.await(termination), exitError);
}),
);
});
20 changes: 15 additions & 5 deletions packages/effect-codex-app-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const isCodexAppServerError = Schema.is(CodexError.CodexAppServerError);
const MAX_BUFFERED_RAW_MESSAGES = 32;
// Bound JSONL lines before joining and parsing; the LF delimiter is excluded.
const MAX_INCOMING_MESSAGE_BYTES = 128 * 1024 * 1024;
const EXIT_STATUS_GRACE = "250 millis";

export interface CodexAppServerProtocolLogEvent {
readonly direction: "incoming" | "outgoing";
Expand Down Expand Up @@ -445,11 +446,20 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
Effect.matchEffect({
onFailure: (error) => handleTermination(() => Effect.succeed(error)),
onSuccess: () =>
handleTermination(
() =>
options.terminationError ??
Effect.succeed(new CodexError.CodexAppServerInputStreamEndedError({})),
),
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),
}),
);
}),
}),
),
}),
Expand Down
Loading