pi-mono/packages/coding-agent/test/exec.test.ts
Gangqiang Yao a83b75a07e
fix(coding-agent): report signal-terminated pi.exec commands as failed (#12089)
Closes #12088.

**Problem.** `pi.exec` mapped a missing exit code to 0 (`code ?? 0`).
Node reports no exit code for a process ended by a signal, so these
commands read as success:

- A timeout or an abort returns `code: 0` and `killed: true`.
- A crash (SIGSEGV) or an outside kill such as the OOM killer returns
`code: 0` and `killed: false`. That is indistinguishable from success.

An extension that checks `result.code === 0` treats these as passed. The
bundled `inline-bash.ts` example is one: it checks `code !== 0`.

**Fix.** Report 128 + the signal number, as a shell does: 143 for
SIGTERM and 137 for SIGKILL. This is the same expression the bash tool
uses (`bash.ts`), and #8994 made the same change in `NodeExecutionEnv`.
If there is neither a code nor a signal, `code` is 1.

Unchanged:

- Normal exit codes.
- `killed` and `truncated`.
- A truncated result is still never 0.

The docs for `pi.exec` and the `ExecResult.code` comment now describe
the signal case.

**Validation** on main `ad335fec19`:

- Three new tests in `test/exec.test.ts` cover a timeout (143), an abort
(143) and a command killing itself with SIGKILL (137, `killed: false`).
All three fail without the fix. They are skipped on Windows, like the
existing signal tests.
- `exec.test.ts`, `exec-utf8-chunks.test.ts` and
`exec-runtime-limit.test.ts` pass.
- `npm run check` passes. In `./test.sh` the only failures are unrelated
to this change:
  - The find tests fail because the sandbox cannot download fd.
  - The pi-env tests need cargo, which the sandbox does not have.
  - One `auth-storage` test also fails on main.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:14:36 +01:00

155 lines
6.2 KiB
TypeScript
Raw Permalink Blame History

import { constants as bufferConstants } from "node:buffer";
import { describe, expect, it } from "vitest";
import { execCommand } from "../src/core/exec.ts";
describe("execCommand", () => {
// Regression test for https://github.com/earendil-works/pi/issues/11085
it("reports spawn errors in stderr", async () => {
const result = await execCommand("pi-command-that-does-not-exist-11085", [], process.cwd());
expect(result).toMatchObject({
code: 1,
killed: false,
truncated: false,
stdout: "",
});
expect(result.stderr).toMatch(/ENOENT/);
});
it("keeps stderr empty when a command exits silently", async () => {
const result = await execCommand(process.execPath, ["-e", "process.exit(1)"], process.cwd());
expect(result).toEqual({ stdout: "", stderr: "", code: 1, killed: false, truncated: false });
});
// Regression tests for https://github.com/earendil-works/pi/issues/11360
it.each([
-1,
0.5,
Number.NaN,
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
bufferConstants.MAX_STRING_LENGTH + 1,
])("rejects invalid maxBuffer %s instead of attempting execution", async (maxBuffer) => {
// A missing command normally resolves to a failed result; invalid options must reject instead.
await expect(
execCommand("pi-command-that-does-not-exist-11360", [], process.cwd(), { maxBuffer }),
).rejects.toThrow(RangeError);
});
it.each([0, bufferConstants.MAX_STRING_LENGTH])("accepts maxBuffer boundary %s", async (maxBuffer) => {
const result = await execCommand(process.execPath, ["-e", ""], process.cwd(), { maxBuffer });
expect(result).toEqual({ stdout: "", stderr: "", code: 0, killed: false, truncated: false });
});
it("truncates all output when maxBuffer is zero", async () => {
const result = await execCommand(process.execPath, ["-e", 'process.stdout.write("x")'], process.cwd(), {
maxBuffer: 0,
});
expect(result).toMatchObject({ stdout: "", stderr: "", truncated: true, killed: true });
expect(result.code).not.toBe(0);
});
it("caps combined output at maxBuffer, terminates the command, and reports truncation", async () => {
const script =
'process.stderr.write("e".repeat(600)); process.stdout.write("o".repeat(600)); setInterval(() => process.stdout.write("x".repeat(1000)), 10);';
const result = await execCommand(process.execPath, ["-e", script], process.cwd(), { maxBuffer: 1000 });
expect(result).toMatchObject({ truncated: true, killed: true });
expect(result.code).not.toBe(0);
expect(Buffer.byteLength(result.stdout) + Buffer.byteLength(result.stderr)).toBe(1000);
});
// #11360: a byte cap must not turn a partial UTF-8 character into replacement output.
it.each([
["stdout", "€", 1, ""],
["stderr", "€", 1, ""],
["stdout", "a€🙂", 5, "a€"],
["stderr", "a€🙂", 5, "a€"],
] as const)(
"keeps complete UTF-8 characters from %s output %s capped at %i bytes",
async (stream, output, maxBuffer, expected) => {
const script = `process.${stream}.write(${JSON.stringify(output)})`;
const result = await execCommand(process.execPath, ["-e", script], process.cwd(), { maxBuffer });
expect(result).toMatchObject({ [stream]: expected, truncated: true, killed: true });
expect(result.code).not.toBe(0);
expect(Buffer.byteLength(result.stdout) + Buffer.byteLength(result.stderr)).toBeLessThanOrEqual(maxBuffer);
},
);
it("still flushes incomplete UTF-8 at normal EOF when output was not truncated", async () => {
const script = "process.stdout.write(Buffer.from([0xe2])); process.stderr.write(Buffer.from([0xf0]));";
const result = await execCommand(process.execPath, ["-e", script], process.cwd());
expect(result).toEqual({ stdout: "<22>", stderr: "<22>", code: 0, killed: false, truncated: false });
});
it("leaves output up to maxBuffer unchanged", async () => {
const result = await execCommand(
process.execPath,
["-e", 'process.stdout.write("a".repeat(1000))'],
process.cwd(),
{
maxBuffer: 1000,
},
);
expect(result).toEqual({ stdout: "a".repeat(1000), stderr: "", code: 0, killed: false, truncated: false });
});
it.skipIf(process.platform === "win32")(
"returns when a shell's child keeps writing after the shell is terminated",
async () => {
// SIGTERM kills only `sh`; `yes` keeps the output pipes open and writing.
const result = await execCommand("sh", ["-c", "yes; echo done"], process.cwd(), { maxBuffer: 1000 });
expect(result).toMatchObject({ killed: true, truncated: true });
expect(result.stdout).toBe("y\n".repeat(500));
},
10_000,
);
// Regression tests for https://github.com/earendil-works/pi/issues/12088
describe.skipIf(process.platform === "win32")("signal-terminated commands", () => {
const idle = ["-e", "setInterval(() => {}, 1000)"];
it("reports 128 + SIGTERM for a timeout", async () => {
const result = await execCommand(process.execPath, idle, process.cwd(), { timeout: 100 });
expect(result).toMatchObject({ code: 143, killed: true, truncated: false });
});
it("reports 128 + SIGTERM for an abort", async () => {
const controller = new AbortController();
setTimeout(() => controller.abort(), 100);
const result = await execCommand(process.execPath, idle, process.cwd(), { signal: controller.signal });
expect(result).toMatchObject({ code: 143, killed: true, truncated: false });
});
it("reports 128 + the signal number for a command killed by someone else", async () => {
// Not killed by pi, so `killed` is false and only the code tells this apart from success.
const script = 'process.kill(process.pid, "SIGKILL")';
const result = await execCommand(process.execPath, ["-e", script], process.cwd());
expect(result).toMatchObject({ code: 137, killed: false, truncated: false });
});
});
it.skipIf(process.platform === "win32")(
"force-kills a command that ignores SIGTERM",
async () => {
// The handler is installed before the first write, so the kill triggered by maxBuffer
// cannot arrive before the child ignores SIGTERM.
const script = 'process.on("SIGTERM", () => {}); process.stdout.write("xx"); setInterval(() => {}, 1000);';
const result = await execCommand(process.execPath, ["-e", script], process.cwd(), { maxBuffer: 1 });
expect(result).toMatchObject({ stdout: "x", killed: true, truncated: true });
expect(result.code).not.toBe(0);
},
15_000,
);
});