diff --git a/computer/agent/run-command.test.ts b/computer/agent/run-command.test.ts index f3579eca..c3348738 100644 --- a/computer/agent/run-command.test.ts +++ b/computer/agent/run-command.test.ts @@ -10,13 +10,24 @@ const base = { describe("runCommand", () => { test("returns output in order and the exit code", async () => { - const result = await runCommand({ ...base, command: "echo one; echo two >&2; exit 3" }); + const result = await runCommand({ ...base, command: "echo one; echo two; exit 3" }); expect(result.output).toBe("one\ntwo\n"); expect(result.exit_code).toBe(3); expect(result.timed_out).toBe(false); }); + /** + * stdout and stderr are two pipes read as they arrive, so which of + * them a line from each lands first is the scheduler's call: both come + * back, each in its own order. + */ + test("brings stderr back together with stdout", async () => { + const result = await runCommand({ ...base, command: "echo out; echo err >&2" }); + + expect(result.output.split("\n").filter(Boolean).sort()).toEqual(["err", "out"]); + }); + test("reads no input", async () => { const result = await runCommand({ ...base, command: "read -r line; echo \"got:$line:$?\"" }); diff --git a/computer/agent/run-command.ts b/computer/agent/run-command.ts index 85ab48e0..7217ad38 100644 --- a/computer/agent/run-command.ts +++ b/computer/agent/run-command.ts @@ -34,7 +34,9 @@ export type CommandResult = { /** * Runs one command in its own `bash -c`, in its own process group, with * no input (a prompt fails at once instead of waiting). stdout and stderr - * come back together, in order, cut at `max_output_bytes`. Past its + * come back together, each in its own order (they are two pipes, read as + * they arrive, so neither is held back for the other), cut at + * `max_output_bytes`. Past its * timeout (when it has one), or when aborted, the whole group gets TERM, * then KILL 5s later. It resolves when bash exits, not when every pipe * closes, so work left in the background (`nohup ... &`) keeps running