bench d166593ec6 opencode: Pi's path guard and the pinned plugin deps instead of a per-rollout install
The bench plugin applies guard-core like Pi's private-home path-guard: file
tools against the allowlist, bash only for root scans and direct shared-
resource commands; external_directory is left to that guard (job.json was
unreadable). Each rollout's config dir now links the pinned
agent/opencode/node_modules and models.json is not fetched: startup no longer
installs ~160 MB over the network.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:54:18 +08:00

112 lines
7.5 KiB
TypeScript

import { tool } from "@opencode-ai/plugin";
import { Schema } from "effect";
import fs from "node:fs";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const terminal = require("../extensions/terminal-core.js"); // same core as Pi's terminal.ts
const {makeGuard, forbiddenCommand, rootFind} = require("../extensions/guard-core.js");
// Pi's path-guard in its private-home branch (the only mode OpenCode runs in): file tools are judged
// against the read/write allowlist; bash is left to the mount jail apart from root scans and direct
// shared-resource commands. Same core, same messages, same guard.log.
function pathGuard(rd: string, workspace: string) {
const bench = process.env.BENCH_ROOT!, split = (k: string) => (process.env[k] ?? "").split(":").filter(Boolean);
const guard = makeGuard({WORKSPACE: workspace, ROLLOUT: rd, JOB_DIR: path.resolve(rd, "..", ".."), BENCH_ROOT: bench,
PROJECT_ROOT: path.resolve(bench, ".."), HOME: process.env.HOME ?? "", MODE: process.env.BENCH_MODE ?? "e2e",
DENY_EXTRA: split("BENCH_GUARD_DENY"), ALLOW_EXTRA: split("BENCH_GUARD_ALLOW")});
const forbidden = ["hb_compile", "hb_mapper", "hb_verifier", "hrt_model_exec", "hb_perf", "ssh", "scp"];
const hint = "请在任务获准的路径内操作;使用工具结果给出的日志路径。";
const deny = (kind: string, detail: string, reason: string) => {
try { fs.appendFileSync(path.join(workspace, "guard.log"), JSON.stringify({ts: Date.now() / 1000, kind, detail: detail.slice(0, 500)}) + "\n"); } catch {}
throw new Error(reason);
};
return (tool: string, args: any) => {
if (["read", "grep", "glob", "write", "edit"].includes(tool)) {
const p = args?.filePath ?? args?.path;
const write = tool === "write" || tool === "edit";
if (!p && write) deny("deny-" + tool, "", "path-guard: 缺少 path");
const v = p && guard.judgePath(p, {write});
if (v) deny("deny-" + tool, p, `path-guard: ${v.reason}${write ? "" : "。" + hint}`);
} else if (tool === "bash") {
const cmd = args?.command ?? "";
if (rootFind(cmd)) deny("deny-bash-root-find", cmd, "不要从 / 扫描服务器;在 workspace、任务返回的日志目录或 $BENCH_ROOT/harness/adapters 内限定搜索。");
const f = forbiddenCommand(cmd, forbidden);
if (f?.uncertain) deny("deny-bash-resource", cmd, "无法静态定位这条命令的共享资源操作;请通过 terminal_exec 的受管入口执行。");
if (f?.command) deny("deny-bash-resource", cmd, `共享资源通过 bench stage run 调度:${f.command}`);
}
};
}
// Only attempt-local evidence is writable; the outer worker owns stages/verdicts.
export default async () => {
const workspace = path.join(process.env.BENCH_ROLLOUT_DIR!, "workspace");
const save = (name: string, value: unknown) => fs.writeFileSync(path.join(workspace, name), JSON.stringify(value));
const definitions: Record<string, unknown> = {};
let turns = 0, tokens = 0;
const seen = new Set<string>();
const guard = pathGuard(process.env.BENCH_ROLLOUT_DIR!, workspace);
const exhausted = () => turns >= Number(process.env.BENCH_MAX_TURNS) || tokens >= Number(process.env.BENCH_MAX_TOKENS);
return {
"tool.definition": async (input: any, output: any) => {
// 1.18.31 registry passes Effect/Zod validators separately from JSON Schema.
if (!output.jsonSchema) {
const doc = Schema.toJsonSchemaDocument(output.parameters);
output.jsonSchema = {...doc.schema, ...(Object.keys(doc.definitions).length ? {$defs:doc.definitions} : {})};
}
if (typeof output.jsonSchema !== "object") throw new Error("bench requires actual tool JSON Schema");
definitions[input.toolID] = {name: input.toolID, description: output.description, parameters: output.jsonSchema};
save("opencode-tools.json", Object.values(definitions));
},
"experimental.chat.system.transform": async (_input: any, output: any) => save("opencode-system.json", output.system),
"chat.params": async () => {
if (exhausted()) throw new Error("bench budget exhausted; no further model request");
},
"tool.execute.before": async (input: any, output: any) => {
if (exhausted() && !["claim", "oe_doc"].includes(input.tool)) throw new Error("bench budget exhausted; finish with a natural final answer");
guard(input.tool, output?.args);
},
event: async ({event}: any) => {
const part = event.properties?.part;
if (part?.type === "step-finish" && !seen.has(part.id)) {
seen.add(part.id); turns++;
const usage = part.tokens || {};
tokens += (usage.input || 0) + (usage.output || 0) + (usage.reasoning || 0); // cache reads excluded, as in Pi
}
},
tool: {
claim: tool({description:"Record your terminal claim; the harness verifies it independently.",
args:{terminal:tool.schema.enum(["PASS","FAIL","BLOCKED"]), summary:tool.schema.string(),
stage:tool.schema.string().optional(), error_class:tool.schema.string().optional(),
artifacts:tool.schema.array(tool.schema.string()).optional()},
execute:async (args) => {
const record={...args,schema:"bench.claim/1",claimed_at:Date.now()/1000};
save("claim.json",record);
fs.appendFileSync(path.join(workspace,"claims.jsonl"),JSON.stringify(record)+"\n");
return `claim recorded: ${args.terminal}`;
}}),
oe_doc: tool({description:"Read OpenExplorer documentation.",args:{query:tool.schema.string()},
execute:async ({query}) => execFileSync(path.join(process.env.BENCH_ROOT!,"agent/oe-doc.sh"),[query],
{encoding:"utf8",timeout:60000,maxBuffer:4<<20}).slice(0,8000)}),
terminal_exec: (() => {
// Same description and parameters as Pi's terminal.ts; a thrown receipt is OpenCode's tool error.
const {commands} = terminal.context(process.env.BENCH_ROLLOUT_DIR);
const z = tool.schema, wait = z.string().regex(/^[a-zA-Z0-9_-]+$/).describe("Existing stage handle from this rollout; no new submission");
return tool({
description: "Wait for an existing stage with wait_handle, without model polling or resubmission. Returns the protected Worker receipt under the original phase/execution deadlines." + (commands ? " Alternatively run a command in the task development environment or assigned board. Paths: /workspace, /out, /input. Formal evaluation uses evaluate=true and predictions JSONL sample_id/top5. Select only an authorized target; CPU tasks use development. Unknown HBM provenance remains UNVERIFIED." : " This task only permits waiting; command execution is disabled."),
args: commands ? {wait_handle: wait.optional(), target: z.enum(["development", "board"]).optional(),
command: z.string().optional(), cwd: z.string().optional(), timeout: z.number().min(1).optional(),
evaluate: z.boolean().optional(), predictions: z.string().optional(),
source: z.string().describe("Workspace subdirectory to freeze for board/formal execution; defaults to .").optional()}
: {wait_handle: wait},
execute: async (args: any, context: any) => {
const params = Object.fromEntries(Object.entries(args).filter(([, v]) => v !== undefined));
return JSON.stringify(await terminal.run(process.env.BENCH_ROLLOUT_DIR, params, context?.abort));
}});
})()
}
};
};