This repository was archived by the owner on Oct 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrun.mjs
More file actions
172 lines (170 loc) · 12.4 KB
/
Copy pathrun.mjs
File metadata and controls
172 lines (170 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
import { readFile, mkdir, writeFile, chmod, rm } from "node:fs/promises";
import { resolve, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { reconcileEvidence } from "./evidence.mjs";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const args = process.argv.slice(2);
const option = (name) => { const i = args.indexOf(name); return i < 0 ? undefined : args[i + 1]; };
const prepareOnly = args.includes("--prepare-only");
const taskId = option("--task");
const model = option("--model");
const arm = option("--arm");
const containerImage = process.env.EVAL_AGENT_IMAGE;
let activeContainer = null;
const activeChildren = new Set();
process.on("SIGTERM", () => {
for (const child of activeChildren) { try { process.kill(-child.pid, "SIGKILL"); } catch { /* exited */ } }
if (activeContainer) spawn("docker", ["rm", "-f", activeContainer], { stdio: "ignore" }).on("close", () => process.exit(143));
else process.exit(143);
});
if (!taskId || !["gpt-6-astra", "gpt-6-sol"].includes(model) || !["medium", "high", "xhigh", "jev"].includes(arm)) {
throw new Error("Usage: node eval/run.mjs --task ID --model gpt-6-astra|gpt-6-sol --arm medium|high|xhigh|jev [--prepare-only]");
}
const manifest = JSON.parse(await readFile(resolve(option("--manifest") ?? join(root, "eval/tasks.json")), "utf8"));
const task = manifest.tasks.find((item) => item.id === taskId);
const remote = typeof task?.repo === "string" && /^https:\/\/github\.com\/[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+\.git$/.test(task.repo);
const grader = task?.grade === "swebench" ? [join(root, "eval/grade-swebench.mjs"), task.id] : task?.grade;
if (!task || !/^[a-zA-Z0-9_-]+$/.test(task.id) || !/^\w{40}$/.test(task.commit) ||
typeof task.repo !== "string" || !(task.repo.startsWith("/") || remote) ||
typeof task.prompt !== "string" || !task.prompt.trim() ||
!Array.isArray(grader) || !grader.length || !grader.every((v) => typeof v === "string" && v.length > 0) ||
!grader[0].startsWith("/") || grader.some((arg) => arg === task.repo || arg.startsWith(`${task.repo}/`)) ||
resolve(task.repo) === root || root.startsWith(`${resolve(task.repo)}/`) ||
(task.agentTimeoutMinutes !== undefined && (!Number.isSafeInteger(task.agentTimeoutMinutes) || task.agentTimeoutMinutes < 1 || task.agentTimeoutMinutes > 60))) {
throw new Error("Task missing or invalid: require pinned repo, commit, prompt, independent absolute grader argv");
}
const runId = `${task.id}-${model}-${arm}-${randomUUID()}`;
const dir = join(root, "eval/runs", runId);
const worktree = join(dir, "worktree");
const taskRepo = remote ? join(dir, "source.git") : task.repo;
await mkdir(dir, { recursive: true, mode: 0o700 });
await chmod(dir, 0o700);
const save = async (name, contents) => writeFile(join(dir, name), contents, { mode: 0o600 });
const run = (command, argv, opts = {}) => new Promise((done, reject) => {
const child = spawn(command, argv, { cwd: opts.cwd ?? root, env: opts.env ?? process.env, stdio: ["ignore", "pipe", "pipe"], detached: true });
activeChildren.add(child);
const stdout = []; const stderr = [];
child.stdout.on("data", (part) => stdout.push(part));
child.stderr.on("data", (part) => stderr.push(part));
let timedOut = false;
const stop = () => { try { process.kill(-child.pid, "SIGKILL"); } catch { /* exited */ } };
const timer = opts.timeoutMs ? setTimeout(() => { timedOut = true; stop(); }, opts.timeoutMs) : null;
child.on("error", (error) => { activeChildren.delete(child); if (timer) clearTimeout(timer); reject(error); });
child.on("close", (code) => { activeChildren.delete(child); if (timer) clearTimeout(timer); done({ code, timedOut, stdout: Buffer.concat(stdout).toString(), stderr: Buffer.concat(stderr).toString() }); });
});
const git = (argv, cwd) => run("git", argv, { cwd });
if (remote) {
const initialized = await git(["init", "--bare", taskRepo]);
if (initialized.code !== 0) throw new Error(`Failed to initialize task source: ${initialized.stderr}`);
const fetched = await git(["-C", taskRepo, "fetch", "--depth=1", "--no-tags", task.repo, task.commit]);
if (fetched.code !== 0) throw new Error(`Failed to fetch pinned task commit: ${fetched.stderr}`);
}
const checkout = await git(["-C", taskRepo, "worktree", "add", "--detach", worktree, task.commit]);
if (checkout.code !== 0) throw new Error(`Failed to create worktree: ${checkout.stderr}`);
const result = { run_id: runId, run_set: process.env.EVAL_RUN_SET ?? null, task: task.id, model, arm, commit: task.commit, prepared: false, grade_passed: null, grader_error: false, elapsed_ms: null, exit_code: null, timed_out: false, requests: 0, efforts: [], fallbacks: 0, input_tokens: null, cached_input_tokens: null, output_tokens: null };
try {
const config = {
$schema: "https://opencode.ai/config.json",
plugin: [[containerImage ? "/router/dist/plugin.js" : join(root, "dist/plugin.js"), {
...(arm === "jev" ? { jevApiKey: "{env:JEV_API_KEY}", jevBaseUrl: process.env.JEV_BASE_URL ?? "https://ai-gateway.vercel.sh/typesafe", maxRetries: 3, fallbackMode: "error", jevTimeoutMs: 10_000 } : { fixedEffort: arm }),
wrap: { openai: [`cliproxy/${model}`] }, decisionsLogPath: join(dir, "decisions.jsonl"),
}]], providers: { cliproxy: { package: "@opencode/ai/providers/openai/responses", settings: { baseURL: process.env.JEV_ROUTER_UPSTREAM_BASE_URL ?? "http://127.0.0.1:8317/v1", apiKey: "{env:CLIPROXY_KEY}" }, models: { [model]: { name: model } } } }, model: `jev-router/${model}`,
};
await save("opencode.json", `${JSON.stringify(config, null, 2)}\n`);
result.prepared = true;
if (!prepareOnly) {
if (!process.env.CLIPROXY_KEY || (arm === "jev" && !process.env.JEV_API_KEY)) throw new Error("Missing CLIPROXY_KEY or JEV_API_KEY");
const home = join(dir, "home");
await mkdir(home, { mode: 0o700 });
for (const name of ["config", "data", "cache", "state"]) await mkdir(join(home, name), { mode: 0o700 });
const start = performance.now();
const agentEnv = { PATH: process.env.PATH, HOME: home, XDG_CONFIG_HOME: join(home, "config"), XDG_DATA_HOME: join(home, "data"), XDG_CACHE_HOME: join(home, "cache"), XDG_STATE_HOME: join(home, "state"),
CLIPROXY_KEY: process.env.CLIPROXY_KEY, ...(arm === "jev" ? { JEV_API_KEY: process.env.JEV_API_KEY } : {}),
OPENCODE_CONFIG: join(dir, "opencode.json"), OPENCODE_DISABLE_PROJECT_CONFIG: "1", OPENCODE_DISABLE_DEFAULT_PLUGINS: "1" };
const containerName = `jev-agent-${randomUUID()}`;
activeContainer = containerImage ? containerName : null;
const dockerArgs = ["run", "--rm", "--name", containerName, "--network", "host", "--user", `${process.getuid()}:${process.getgid()}`,
"--mount", `type=bind,src=${dir},dst=${dir}`, "--mount", `type=bind,src=${process.env.EVAL_BUILD_DIR ?? join(root, "dist")},dst=/router/dist,readonly`,
"--mount", `type=bind,src=${join(root, "node_modules")},dst=/router/node_modules,readonly`,
"--mount", `type=bind,src=${process.env.EVAL_OPENCODE_BIN ?? "/home/robert/.opencode/bin/opencode"},dst=/usr/local/bin/opencode,readonly`,
"--tmpfs", "/tmp:rw,nosuid,nodev,size=512m", "--workdir", worktree,
...Object.entries(agentEnv).filter(([, value]) => value !== undefined).flatMap(([key, value]) => ["--env", `${key}=${value}`]),
containerImage, "opencode", "run", "--dir", worktree, "--model", `jev-router/${model}`, "--format", "json", task.prompt];
let oc;
try { oc = await run(containerImage ? "docker" : "opencode", containerImage ? dockerArgs : ["run", "--dir", worktree, "--model", `jev-router/${model}`, "--format", "json", task.prompt], {
cwd: worktree, timeoutMs: (task.agentTimeoutMinutes ?? 15) * 60_000,
env: containerImage ? process.env : agentEnv,
}); } finally {
if (containerImage) await run("docker", ["rm", "-f", containerName], { timeoutMs: 15_000 });
activeContainer = null;
}
result.elapsed_ms = Math.round(performance.now() - start);
result.exit_code = oc.code; result.timed_out = oc.timedOut;
await save("output.jsonl", oc.stdout);
await save("stderr.log", oc.stderr);
// Intent-to-add captures new agent files without staging their contents.
const added = await git(["add", "-N", "."], worktree);
if (added.code !== 0) throw new Error(`Failed to capture new files: ${added.stderr}`);
const patch = await git(["diff", "--binary", task.commit], worktree);
if (patch.code !== 0) throw new Error(`Failed to capture patch: ${patch.stderr}`);
await save("patch.diff", patch.stdout);
const agentError = oc.stdout.split("\n").some((line) => {
try { return JSON.parse(line).type === "error"; } catch { return false; }
});
result.agent_error = agentError;
let classificationErrors = 0;
try {
classificationErrors = (await readFile(join(dir, "decisions.jsonl"), "utf8")).trim().split("\n").map(JSON.parse).filter((e) => e.outcome === "classification_failed").length;
} catch { /* Missing evidence is rejected during reconciliation below. */ }
result.classification_errors = classificationErrors;
if (oc.code === 0 && !oc.timedOut && !agentError && !classificationErrors) {
// The grader runs outside the agent-writable checkout and receives only
// its patch and pinned source. An adapter must apply the patch to a fresh
// checkout and use tests that are not taken from agent-modified files.
const dockerEnv = Object.fromEntries(["DOCKER_HOST", "DOCKER_CONFIG", "DOCKER_CERT_PATH", "DOCKER_TLS_VERIFY"].filter((key) => process.env[key]).map((key) => [key, process.env[key]]));
const grade = await run(grader[0], grader.slice(1), { cwd: dir, timeoutMs: task.grade === "swebench" ? 30 * 60_000 : 120_000,
env: { PATH: process.env.PATH, HOME: process.env.HOME, ...dockerEnv,
...(process.env.SWE_BENCH_DATASET_PATH ? { SWE_BENCH_DATASET_PATH: process.env.SWE_BENCH_DATASET_PATH } : {}),
...(process.env.SWE_BENCH_PYTHON ? { SWE_BENCH_PYTHON: process.env.SWE_BENCH_PYTHON } : {}),
...(process.env.EVAL_DATASET_DIGEST_FILE ? { EVAL_DATASET_DIGEST_FILE: process.env.EVAL_DATASET_DIGEST_FILE } : {}),
EVAL_PATCH_PATH: join(dir, "patch.diff"), EVAL_TASK_REPO: taskRepo, EVAL_TASK_COMMIT: task.commit } });
result.grader_error = grade.timedOut || (grade.code !== 0 && grade.code !== 1);
result.grade_passed = result.grader_error ? null : grade.code === 0;
await save("grade.log", grade.stdout + grade.stderr);
} // An incomplete agent run was not submitted to the benchmark grader.
// Decision logging is asynchronous; allow the queue to flush after OpenCode exits.
let evidence = ""; let stable = 0;
for (let i = 0; i < 30; i++) {
await new Promise((done) => setTimeout(done, 100));
let next = "";
try { next = await readFile(join(dir, "decisions.jsonl"), "utf8"); } catch { /* no decisions yet */ }
stable = next && next === evidence ? stable + 1 : 0;
evidence = next;
if (stable >= 3) break;
}
const events = evidence.trim().split("\n").filter(Boolean).map(JSON.parse);
result.requests = events.length;
result.efforts = events.map((e) => e.effort);
result.fallbacks = events.filter((e) => e.fallback).length;
for (const field of ["input_tokens", "cached_input_tokens", "output_tokens"]) {
if (events.length && events.every((e) => Number.isFinite(e[field]))) result[field] = events.reduce((sum, e) => sum + e[field], 0);
}
const output = oc.stdout.split("\n").filter(Boolean).flatMap((line) => {
try { return [JSON.parse(line)]; } catch { return []; }
});
const reconciliation = reconcileEvidence(events, output);
result.auxiliary_requests = reconciliation?.auxiliary ?? null;
result.evidence_valid = events.length > 0 && events.every((e) => e.model === model && e.outcome === "completed" && (arm === "jev" || e.effort === arm)) &&
reconciliation !== null;
result.agent_usage = result.evidence_valid ? reconciliation.agent : null;
if (!result.evidence_valid) { result.input_tokens = null; result.cached_input_tokens = null; result.output_tokens = null; }
}
} finally {
await save("result.json", `${JSON.stringify(result, null, 2)}\n`);
const removed = await git(["-C", taskRepo, "worktree", "remove", "--force", worktree]);
if (removed.code !== 0) console.error(`Worktree cleanup failed: ${removed.stderr}`);
if (remote) await (await import("node:fs/promises")).rm(taskRepo, { recursive: true, force: true });
}
console.log(join(dir, "result.json"));