Skip to content
Open
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
36 changes: 29 additions & 7 deletions dist/main.js

Large diffs are not rendered by default.

37 changes: 31 additions & 6 deletions src/runCodexExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,47 @@ export async function runCodexExec({
env,
stdio: ["pipe", "inherit", "inherit"],
});
child.stdin.write(input);
child.stdin.end();
let settled = false;

child.on("error", reject);
const rejectOnce = (err: unknown) => {
if (settled) {
return;
}
settled = true;
reject(err);
};

const resolveOnce = () => {
if (settled) {
return;
}
settled = true;
resolve(undefined);
};

child.stdin.on("error", (err) => {
rejectOnce(
new Error(`failed to write prompt to ${program} stdin: ${err.message}`)
);
});
child.stdin.end(input);

child.on("error", rejectOnce);

child.on("close", async (code) => {
if (settled) {
return;
}
if (code !== 0) {
reject(new Error(`${program} exited with code ${code}`));
rejectOnce(new Error(`${program} exited with code ${code}`));
return;
}

try {
await finalizeExecution(outputFile, runAsUser);
resolve(undefined);
resolveOnce();
} catch (err) {
reject(err);
rejectOnce(err);
}
});
});
Expand Down
33 changes: 30 additions & 3 deletions test/runCodexExec.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ function runCodexExecWithFakeCodex({
permissionProfile = "",
extraArgs = "",
safetyStrategy = "unsafe",
prompt = "test prompt",
promptFileContents = null,
fakeCodexSource = null,
} = {}) {
const tempDir = mkdtempSync(path.join(tmpdir(), "codex-action-permissions-"));
const capturePath = path.join(tempDir, "args.json");
const outputPath = path.join(tempDir, "output.txt");
const fakeCodexPath = path.join(tempDir, "codex.mjs");
const promptPath = path.join(tempDir, "prompt.txt");
if (promptFileContents != null) {
writeFileSync(promptPath, promptFileContents, "utf8");
}
writeFileSync(
fakeCodexPath,
`import { writeFileSync } from "node:fs";
fakeCodexSource ?? `import { writeFileSync } from "node:fs";
const args = process.argv.slice(2);
writeFileSync(process.env.CODEX_CAPTURE_ARGS, JSON.stringify(args));
const outputIndex = args.indexOf("--output-last-message");
Expand Down Expand Up @@ -62,9 +69,9 @@ writeFileSync(args[outputIndex + 1], "fake final message\\n");
mainPath,
"run-codex-exec",
"--prompt",
"test prompt",
promptFileContents == null ? prompt : "",
"--prompt-file",
"",
promptFileContents == null ? "" : promptPath,
"--codex-home",
"",
"--cd",
Expand Down Expand Up @@ -111,6 +118,26 @@ writeFileSync(args[outputIndex + 1], "fake final message\\n");
return { result, capturedArgs };
}

test("reports stdin EPIPE as a controlled run-codex-exec failure", () => {
const { result, capturedArgs } = runCodexExecWithFakeCodex({
promptFileContents: "x".repeat(64 * 1024 * 1024),
fakeCodexSource: `import { writeFileSync } from "node:fs";
const args = process.argv.slice(2);
writeFileSync(process.env.CODEX_CAPTURE_ARGS, JSON.stringify(args));
process.stdin.destroy();
process.exit(7);
`,
});

assert.notEqual(result.status, 0);
assert.ok(capturedArgs.includes("exec"));
assert.match(
result.stderr,
/failed to write prompt to codex stdin: write EPIPE|codex exited with code 7/
);
assert.doesNotMatch(result.stderr, /Unhandled 'error' event/);
});

test("preserves workspace-write as the default legacy sandbox", () => {
const { result, capturedArgs } = runCodexExecWithFakeCodex();

Expand Down
Loading