opencode-bench/test/graders.test.ts
KaKi87 6e3b52f045 Split time per task into model time and the rest
Model time comes from the step and tool timestamps in opencode's JSON events.
A backfill command fills it for older results from their saved event logs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-25 10:31:54 +01:00

52 lines
2.3 KiB
TypeScript

import { expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { tmpdir } from "node:os";
import { benchmarks } from "../src/benchmarks";
import { parseEvents } from "../src/runner";
import { SOURCES } from "../src/benchmarks/datasets";
// An agent that does nothing must never pass.
// Imported datasets are covered by datasets.test.ts.
for (const b of benchmarks.filter((x) => !(x.id in SOURCES))) {
for (const t of b.tasks()) {
test(`${b.id}/${t.id} fails on an untouched workspace`, async () => {
const dir = mkdtempSync(join(tmpdir(), "grade-"));
for (const [f, c] of Object.entries(t.files ?? {})) {
mkdirSync(dirname(join(dir, f)), { recursive: true });
writeFileSync(join(dir, f), c);
}
writeFileSync(join(dir, ".grade.py"), t.grade);
const p = Bun.spawn(["python3", "-I", ".grade.py"], { cwd: dir, stdout: "pipe", stderr: "pipe" });
expect(await p.exited).not.toBe(0);
});
}
}
test("parseEvents sums tokens and counts steps", () => {
const jsonl = [
'{"type":"step_start"}',
'{"type":"step_finish","part":{"tokens":{"input":10,"output":5,"reasoning":2,"cache":{"read":3,"write":1}}}}',
'{"type":"step_finish","part":{"tokens":{"input":1,"output":1,"reasoning":0,"cache":{"read":0,"write":0}}}}',
"not json",
].join("\n");
expect(parseEvents(jsonl)).toMatchObject({ steps: 2, tin: 15, tout: 6, treason: 2, cost: 0 });
});
test("parseEvents sums reported cost", () => {
const line = (cost: number) => JSON.stringify({ type: "step_finish", part: { cost, tokens: {} } });
expect(parseEvents([line(0.01), line(0.02)].join("\n")).cost).toBeCloseTo(0.03);
});
test("parseEvents separates model time from tool time", () => {
const jsonl = [
'{"type":"step_start","timestamp":1000}',
'{"type":"tool_use","timestamp":9000,"part":{"state":{"time":{"start":8000,"end":9000}}}}',
'{"type":"step_finish","timestamp":9002,"part":{"tokens":{}}}',
'{"type":"step_start","timestamp":9500}',
'{"type":"step_finish","timestamp":10500,"part":{"tokens":{}}}',
].join("\n");
// Step 1 is 8002 ms with a 1000 ms tool run, step 2 is 1000 ms with no tool.
expect(parseEvents(jsonl).modelMs).toBe(7002 + 1000);
expect(parseEvents('{"type":"error"}').modelMs).toBeNull();
});