Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

[codex] Preserve child process termination context (#3469)

Julius Marmingecebbe6ff193c
diff --git a/packages/effect-acp/src/_internal/stdio.test.ts b/packages/effect-acp/src/_internal/stdio.test.ts
new file mode 100644
index 000000000..8a4171f7a
--- /dev/null
+++ b/packages/effect-acp/src/_internal/stdio.test.ts
@@ -0,0 +1,45 @@
+import { assert, describe, it } from "@effect/vitest";
+import * as Effect from "effect/Effect";
+import * as PlatformError from "effect/PlatformError";
+import { ChildProcessSpawner } from "effect/unstable/process";
+
+import * as AcpError from "../errors.ts";
+import { makeTerminationError } from "./stdio.ts";
+
+describe("ACP child process termination", () => {
+ it.effect("retains the process identifier with the exit code", () =>
+ Effect.gen(function* () {
+ const error = yield* makeTerminationError({
+ pid: ChildProcessSpawner.ProcessId(41),
+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(7)),
+ });
+
+ assert.instanceOf(error, AcpError.AcpProcessExitedError);
+ assert.equal(error.pid, 41);
+ assert.equal(error.code, 7);
+ assert.equal(error.message, "ACP process exited with code 7");
+ }),
+ );
+
+ it.effect("retains the process identifier and exact exit-status cause", () =>
+ Effect.gen(function* () {
+ const rootCause = new Error("private process diagnostics");
+ const cause = PlatformError.systemError({
+ _tag: "Unknown",
+ module: "ChildProcess",
+ method: "exitCode",
+ cause: rootCause,
+ });
+ const error = yield* makeTerminationError({
+ pid: ChildProcessSpawner.ProcessId(42),
+ exitCode: Effect.fail(cause),
+ });
+
+ assert.instanceOf(error, AcpError.AcpTransportError);
+ assert.equal(error.pid, 42);
+ assert.strictEqual(error.cause, cause);
+ assert.equal(error.message, "ACP transport operation read-process-exit-status failed.");
+ assert.notInclude(error.message, rootCause.message);
+ }),
+ );
+});
diff --git a/packages/effect-acp/src/_internal/stdio.ts b/packages/effect-acp/src/_internal/stdio.ts
index 393a1c591..87af63363 100644
--- a/packages/effect-acp/src/_internal/stdio.ts
+++ b/packages/effect-acp/src/_internal/stdio.ts
@@ -44,14 +44,20 @@ export const makeInMemoryStdio = Effect.fn("makeInMemoryStdio")(function* () {
};
});
+type ChildProcessTerminationHandle = Pick<
+ ChildProcessSpawner.ChildProcessHandle,
+ "exitCode" | "pid"
+>;
+
export const makeTerminationError = (
- handle: ChildProcessSpawner.ChildProcessHandle,
+ handle: ChildProcessTerminationHandle,
): Effect.Effect<AcpError.AcpError> =>
Effect.match(handle.exitCode, {
onFailure: (cause) =>
new AcpError.AcpTransportError({
operation: "read-process-exit-status",
+ pid: handle.pid,
cause,
}),
- onSuccess: (code) => new AcpError.AcpProcessExitedError({ code }),
+ onSuccess: (code) => new AcpError.AcpProcessExitedError({ code, pid: handle.pid }),
});
diff --git a/packages/effect-acp/src/errors.ts b/packages/effect-acp/src/errors.ts
index 3fe0a4690..f92568a14 100644
--- a/packages/effect-acp/src/errors.ts
+++ b/packages/effect-acp/src/errors.ts
@@ -91,6 +91,7 @@ export class AcpProcessExitedError extends Schema.TaggedErrorClass<AcpProcessExi
"AcpProcessExitedError",
{
code: Schema.optional(Schema.Number),
+ pid: Schema.optionalKey(Schema.Int),
cause: Schema.optional(Schema.Defect()),
},
) {
@@ -160,6 +161,7 @@ export class AcpTransportError extends Schema.TaggedErrorClass<AcpTransportError
),
method: Schema.optional(Schema.String),
detail: Schema.optional(Schema.String),
+ pid: Schema.optionalKey(Schema.Int),
cause: Schema.Defect(),
},
) {
diff --git a/packages/effect-codex-app-server/src/_internal/stdio.test.ts b/packages/effect-codex-app-server/src/_internal/stdio.test.ts
new file mode 100644
index 000000000..6dbf4c857
--- /dev/null
+++ b/packages/effect-codex-app-server/src/_internal/stdio.test.ts
@@ -0,0 +1,48 @@
+import { assert, describe, it } from "@effect/vitest";
+import * as Effect from "effect/Effect";
+import * as PlatformError from "effect/PlatformError";
+import { ChildProcessSpawner } from "effect/unstable/process";
+
+import * as CodexError from "../errors.ts";
+import { makeTerminationError } from "./stdio.ts";
+
+describe("Codex App Server child process termination", () => {
+ it.effect("retains the process identifier with the exit code", () =>
+ Effect.gen(function* () {
+ const error = yield* makeTerminationError({
+ pid: ChildProcessSpawner.ProcessId(51),
+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(9)),
+ });
+
+ assert.instanceOf(error, CodexError.CodexAppServerProcessExitedError);
+ assert.equal(error.pid, 51);
+ assert.equal(error.code, 9);
+ assert.equal(error.message, "Codex App Server process exited with code 9");
+ }),
+ );
+
+ it.effect("retains the process identifier and exact exit-status cause", () =>
+ Effect.gen(function* () {
+ const rootCause = new Error("private process diagnostics");
+ const cause = PlatformError.systemError({
+ _tag: "Unknown",
+ module: "ChildProcess",
+ method: "exitCode",
+ cause: rootCause,
+ });
+ const error = yield* makeTerminationError({
+ pid: ChildProcessSpawner.ProcessId(52),
+ exitCode: Effect.fail(cause),
+ });
+
+ assert.instanceOf(error, CodexError.CodexAppServerTransportError);
+ assert.equal(error.pid, 52);
+ assert.strictEqual(error.cause, cause);
+ assert.equal(
+ error.message,
+ "Codex App Server transport operation 'read-process-exit-status' failed.",
+ );
+ assert.notInclude(error.message, rootCause.message);
+ }),
+ );
+});
diff --git a/packages/effect-codex-app-server/src/_internal/stdio.ts b/packages/effect-codex-app-server/src/_internal/stdio.ts
index 9167129db..312022824 100644
--- a/packages/effect-codex-app-server/src/_internal/stdio.ts
+++ b/packages/effect-codex-app-server/src/_internal/stdio.ts
@@ -44,14 +44,20 @@ export const makeInMemoryStdio = Effect.fn("makeInMemoryStdio")(function* () {
};
});
+type ChildProcessTerminationHandle = Pick<
+ ChildProcessSpawner.ChildProcessHandle,
+ "exitCode" | "pid"
+>;
+
export const makeTerminationError = (
- handle: ChildProcessSpawner.ChildProcessHandle,
+ handle: ChildProcessTerminationHandle,
): Effect.Effect<CodexError.CodexAppServerError> =>
Effect.match(handle.exitCode, {
onFailure: (cause) =>
new CodexError.CodexAppServerTransportError({
operation: "read-process-exit-status",
+ pid: handle.pid,
cause,
}),
- onSuccess: (code) => new CodexError.CodexAppServerProcessExitedError({ code }),
+ onSuccess: (code) => new CodexError.CodexAppServerProcessExitedError({ code, pid: handle.pid }),
});
diff --git a/packages/effect-codex-app-server/src/errors.ts b/packages/effect-codex-app-server/src/errors.ts
index 3826a0992..f0e0945d3 100644
--- a/packages/effect-codex-app-server/src/errors.ts
+++ b/packages/effect-codex-app-server/src/errors.ts
@@ -146,6 +146,7 @@ export class CodexAppServerProcessExitedError extends Schema.TaggedErrorClass<Co
"CodexAppServerProcessExitedError",
{
code: Schema.optional(Schema.Number),
+ pid: Schema.optionalKey(Schema.Int),
cause: Schema.optional(Schema.Defect()),
},
) {
@@ -233,6 +234,7 @@ export class CodexAppServerTransportError extends Schema.TaggedErrorClass<CodexA
"CodexAppServerTransportError",
{
operation: CodexAppServerTransportOperation,
+ pid: Schema.optionalKey(Schema.Int),
cause: Schema.Defect(),
},
) {