fix(agent): make timeout output test deterministic

This commit is contained in:
Armin Ronacher 2026-08-01 14:06:00 +02:00
parent a116523434
commit a1403af8ec

View file

@ -7,7 +7,8 @@ import { createEditTool } from "../../src/harness/tools/edit.ts";
import { createReadTool } from "../../src/harness/tools/read.ts";
import { createWriteTool } from "../../src/harness/tools/write.ts";
import {
type ExecutionError,
ExecutionError,
err,
type FileError,
getOrThrow,
ok,
@ -100,6 +101,16 @@ class LateOutputExecutionEnv extends NodeExecutionEnv {
}
}
class TimeoutWithOutputExecutionEnv extends NodeExecutionEnv {
override async exec(
_command: string,
options?: ShellExecOptions,
): Promise<Result<{ stdout: string; stderr: string; exitCode: number }, ExecutionError>> {
options?.onStdout?.(`${Array.from({ length: 3000 }, (_, index) => `line-${index + 1}`).join("\n")}\n`);
return err(new ExecutionError("timeout", "timed out"));
}
}
function createTinyBmp(): Uint8Array {
const bytes = new Uint8Array(58);
const view = new DataView(bytes.buffer);
@ -475,18 +486,15 @@ describe("AgentHarness tools", () => {
});
it("preserves truncated output when a command times out", async () => {
const context = createContext();
const env = new TimeoutWithOutputExecutionEnv({ cwd: createTempDir() });
let error: unknown;
try {
await createBashTool().execute(
"bash-timeout-output",
{
command: "i=1; while [ $i -le 3000 ]; do echo line-$i; i=$((i + 1)); done; sleep 2",
timeout: 0.05,
},
{ command: "emit output and time out", timeout: 0.05 },
undefined,
undefined,
context,
{ env },
);
} catch (cause) {
error = cause;
@ -497,7 +505,7 @@ describe("AgentHarness tools", () => {
expect(message).toContain("Command timed out after 0.05 seconds");
const fullOutputPath = message.match(/Full output: ([^\]\n]+)/)?.[1];
expect(fullOutputPath).toBeDefined();
const fullOutput = getOrThrow(await context.env.readTextFile(fullOutputPath!));
const fullOutput = getOrThrow(await env.readTextFile(fullOutputPath!));
expect(fullOutput).toContain("line-1\nline-2");
expect(fullOutput).toContain("line-2999\nline-3000");
});