The agent could read and copy harness/adapters (model loading, preprocessing, export, decoding), and every bench dev container carried the whole harness, registry, scripts and export trees, evaluator and gates included. That did the model-specific half of the deployment for it. - guard and private-home jail no longer expose harness/adapters; bench dev packs only container_site - the evaluator still decodes outputs with the platform's own code, so agent/io-contracts states what the graph's inputs and outputs mean (9 families, 71 models so far); the prompt appends the task's contract - SYSTEM.md: says where official sources, weights and environments are; the compile.yaml template is replaced by the facts that differ from the docs - e2e prompt: whole-class int16/float fallback and >1.5x board latency do not pass - agent/VERSION 24 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
210 lines
11 KiB
TypeScript
210 lines
11 KiB
TypeScript
/**
|
||
* path-guard — agent 的读写边界(判定核心在 guard-core.js,纯函数,node 可单测:node agent/extensions/guard-core.test.js)。
|
||
*
|
||
* v1 是黑名单正则(ORACLE_RX + 重定向 + 写命令),另一个会话实测 5 种绕过:
|
||
* `cd ../.. && cat oracle/…` · `cd ..;cd ..;head oracle/…` · `ls ../../ora*` · `find ../.. -exec cat` · `cat ../01/claim.json`(兄弟 rollout)。
|
||
* v2 改成**读白名单 + cd 禁用**:
|
||
* read / grep / write / edit 的 path 参数 → guard.judgePath;
|
||
* bash → 命令里含 cd/pushd/popd 直接拒,再把所有像路径的 token(相对路径按 cwd=workspace 解析、glob 取前缀)逐个判。
|
||
* 保留 v1 的:写目标(cp/mv/rm/tee…最后参数 / 全部参数)与重定向目标必须在 workspace(或 /tmp、/dev/null);
|
||
* 禁止直接调 hb_compile 等重命令(走 bench stage run);`sleep 100+`、`git push`。
|
||
*
|
||
* 白名单(e2e):workspace/(可写)· 本 rollout 根(agent/ stages/ handles/ rollout.json)· <job>/job.json · <job>/data
|
||
* · bench/data · bench/cache · bench/bin · bench/agent/oe-doc.sh · bench/agent/speed_ref · <proj>/.horizon · <proj>/AGENTS.md ;repair 额外 <job>/oracle。
|
||
* 项目树内其余一律拒(别的 rollout、harness/、state/、export/、别的 job);项目树外只拒 ~/.pi、~/toolchains、bench/.env.local。
|
||
*
|
||
* 每次拒绝写一行到 <rollout>/agent/guard.log(L4 导出统计"越界尝试次数")。第二道保险是文件系统权限。
|
||
*/
|
||
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
||
import * as fs from "node:fs";
|
||
import * as path from "node:path";
|
||
import { createRequire } from "node:module";
|
||
|
||
const _require = createRequire(__filename);
|
||
const { makeGuard, forbiddenCommand, rootFind } = _require("./guard-core.js");
|
||
|
||
const ROLLOUT = process.env.BENCH_ROLLOUT_DIR ?? "";
|
||
const MODE = process.env.BENCH_MODE ?? "e2e";
|
||
// Set by jail_setup only after mounting the trusted recorded private-home policy.
|
||
const PRIVATE_JAIL = process.env.BENCH_JAILED === "1" && process.env.BENCH_JAIL_POLICY === "private-home";
|
||
const BENCH_ROOT = process.env.BENCH_ROOT ?? path.resolve(__dirname, "..", "..");
|
||
const WORKSPACE = ROLLOUT ? path.resolve(ROLLOUT, "workspace") : process.cwd();
|
||
const JOB_DIR = ROLLOUT ? path.resolve(ROLLOUT, "..", "..") : path.resolve(WORKSPACE, "..", "..", "..");
|
||
const GUARD_LOG = ROLLOUT ? path.resolve(ROLLOUT, process.env.BENCH_JAILED === "1" ? "workspace" : "agent", "guard.log") : "";
|
||
|
||
// harness/rollout.py agent_env 传入:BENCH_GUARD_DENY = realpath(state):work_root(都是指向 /home 的链接/本地盘,绕开项目树白名单);
|
||
// BENCH_GUARD_ALLOW = 本 rollout 在 work_root 下的编译工作目录。
|
||
// env 缺失时自己推:realpath(bench/state)、config.yaml 的 paths.work_root、以及 work_root/<run>/<job>/r<kk>(从 ROLLOUT 路径倒推)。
|
||
// 这样 guard 不依赖 harness 版本(正在跑的旧 collect 进程不传这两个 env 也一样严)。
|
||
const splitEnv = (k: string) => (process.env[k] ?? "").split(":").filter(Boolean);
|
||
function guessWorkRoot(): string {
|
||
try {
|
||
const m = /^\s*work_root:\s*(\S+)/m.exec(fs.readFileSync(path.join(BENCH_ROOT, "config.yaml"), "utf8"));
|
||
if (!m) return "";
|
||
return m[1].replace(/^['"]|['"]$/g, "").replace(/^~/, process.env.HOME ?? "");
|
||
} catch { return ""; }
|
||
}
|
||
function realOrSelf(p: string): string { try { return fs.realpathSync(p); } catch { return p; } }
|
||
let DENY_EXTRA = splitEnv("BENCH_GUARD_DENY");
|
||
let ALLOW_EXTRA = splitEnv("BENCH_GUARD_ALLOW");
|
||
if (!DENY_EXTRA.length) {
|
||
const wr = guessWorkRoot();
|
||
DENY_EXTRA = [realOrSelf(path.join(BENCH_ROOT, "state")), wr].filter(Boolean);
|
||
if (wr && ROLLOUT && !ALLOW_EXTRA.length) {
|
||
const rel = path.relative(path.join(BENCH_ROOT, "runs"), ROLLOUT).split(path.sep); // <run>/<job>/rollouts/<kk>
|
||
if (rel.length === 4 && rel[2] === "rollouts" && !rel[0].startsWith(".."))
|
||
ALLOW_EXTRA = [path.join(wr, rel[0], rel[1], "r" + rel[3].padStart(2, "0"))];
|
||
}
|
||
}
|
||
const guard = makeGuard({
|
||
WORKSPACE, ROLLOUT: ROLLOUT || path.resolve(WORKSPACE, ".."), JOB_DIR,
|
||
BENCH_ROOT, PROJECT_ROOT: path.resolve(BENCH_ROOT, ".."),
|
||
HOME: process.env.HOME ?? "", MODE, DENY_EXTRA, ALLOW_EXTRA,
|
||
});
|
||
|
||
|
||
function inside(p: string): boolean {
|
||
const abs = path.resolve(WORKSPACE, p);
|
||
return abs === WORKSPACE || abs.startsWith(WORKSPACE + path.sep);
|
||
}
|
||
|
||
function log(kind: string, detail: string) {
|
||
if (!GUARD_LOG) return;
|
||
try {
|
||
fs.mkdirSync(path.dirname(GUARD_LOG), { recursive: true });
|
||
fs.appendFileSync(GUARD_LOG, JSON.stringify({ ts: Date.now() / 1000, kind, detail }) + "\n");
|
||
} catch { /* 日志失败不影响拒绝 */ }
|
||
}
|
||
log("init", JSON.stringify({ mode: MODE, deny_extra: DENY_EXTRA, allow_extra: ALLOW_EXTRA })); // trace.py 统计拦截数时跳过 kind=init
|
||
|
||
// Lexical diagnostics respect quoted search data. Resource admission and actual
|
||
// filesystem boundaries are enforced by the managed worker and mount jail.
|
||
// Legacy bash stays conservative while the managed terminal is rolled out.
|
||
const FORBIDDEN_CMDS = ["hb_compile", "hb_mapper", "hb_verifier", "hrt_model_exec", "hb_perf",
|
||
"ssh", "scp", ...(PRIVATE_JAIL ? [] : ["rsync", "wget", "curl", "pip", "pip3", "conda", "npm"])];
|
||
const WRITE_PATTERNS: RegExp[] = [
|
||
/\bsleep\s+[0-9]{3,}/, // 用 terminal_exec wait_handle,不要 sleep 等编译
|
||
/\bgit\s+push\b/,
|
||
];
|
||
|
||
// 写文件的命令:DEST_LAST 的最后一个参数是目的地(cp <job>/job.json . 是合法的"读进来");ALL_ARGS 每个非选项参数都是目标。
|
||
const DEST_LAST = new Set(["cp", "mv", "ln", "install", "rsync"]);
|
||
const ALL_ARGS = new Set(["rm", "rmdir", "tee", "touch", "mkdir", "chmod", "chown", "truncate", "dd", "shred"]);
|
||
|
||
function writeTargets(segment: string): string[] {
|
||
const toks = segment.trim().split(/\s+/).filter(Boolean);
|
||
let i = 0;
|
||
while (i < toks.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(toks[i])) i++; // 跳过 env 赋值
|
||
while (i < toks.length && ["sudo", "env", "nohup", "time"].includes(toks[i])) i++;
|
||
const cmd = (toks[i] ?? "").replace(/^.*\//, "");
|
||
const args = toks.slice(i + 1).filter((t) => !t.startsWith("-")).map((t) => t.replace(/^['"]|['"]$/g, ""));
|
||
if (DEST_LAST.has(cmd)) return args.length ? [args[args.length - 1]] : [];
|
||
if (ALL_ARGS.has(cmd)) return args;
|
||
return [];
|
||
}
|
||
|
||
function badWriteTarget(cmd: string): string | null {
|
||
for (const seg of cmd.split(/;|\|\||&&|\||&/)) {
|
||
for (const t of writeTargets(seg)) {
|
||
if (t.startsWith("/tmp/") || t === "/dev/null") continue;
|
||
if (!inside(t)) return t;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
const REDIRECT_OK = new Set(["/dev/null", "/dev/stderr", "/dev/stdout"]);
|
||
|
||
/** 所有输出重定向目标(> >> 2> &>),有一个落在 workspace 外就拒。cd 已被整体禁用,所以相对路径按 cwd=workspace 解析是可靠的。 */
|
||
function badRedirect(cmd: string): string | null {
|
||
const re = /(?:^|[\s;|&(])(?:\d?>>?|&>)\s*['"]?([^\s'"|;&)]+)/g;
|
||
let m: RegExpExecArray | null;
|
||
while ((m = re.exec(cmd)) !== null) {
|
||
const target = m[1];
|
||
if (target.startsWith("&")) continue; // 2>&1
|
||
if (REDIRECT_OK.has(target)) continue;
|
||
if (target.startsWith("/proc/self/fd/")) continue;
|
||
if (target.startsWith("/tmp/")) continue;
|
||
if (!inside(target)) return target;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
const HINT = "请在任务获准的路径内操作;使用工具结果给出的日志路径。";
|
||
|
||
export default function (pi: ExtensionAPI) {
|
||
pi.on("tool_call", async (event: any, _ctx: any) => {
|
||
const name: string = event.toolName ?? event.tool ?? "";
|
||
const input: any = event.input ?? {};
|
||
|
||
// 文件类工具:read / grep 只读判定,write / edit 写判定
|
||
if (name === "read" || name === "grep") {
|
||
const p: string = input.path ?? input.file_path ?? "";
|
||
if (p) {
|
||
const v = guard.judgePath(p);
|
||
if (v) {
|
||
log("deny-" + name, p);
|
||
return { block: true, reason: `path-guard: ${v.reason}。${HINT}` };
|
||
}
|
||
}
|
||
return undefined;
|
||
}
|
||
if (name === "write" || name === "edit") {
|
||
const p: string = input.path ?? input.file_path ?? "";
|
||
const v = p ? guard.judgePath(p, { write: true }) : { reason: "缺少 path" };
|
||
if (v) {
|
||
log("deny-" + name, p);
|
||
return { block: true, reason: `path-guard: ${v.reason}` };
|
||
}
|
||
return undefined;
|
||
}
|
||
if (name !== "bash") return undefined;
|
||
|
||
const cmd: string = input.command ?? "";
|
||
if (rootFind(cmd)) {
|
||
log("deny-bash-root-find", cmd.slice(0, 500));
|
||
return { block: true, reason: "不要从 / 扫描服务器;在 workspace 或任务返回的日志目录内限定搜索。" };
|
||
}
|
||
// The private mount jail enforces filesystem boundaries. The lexer here is
|
||
// only a diagnostic for direct shared-resource commands, never isolation.
|
||
if (PRIVATE_JAIL) {
|
||
const forbidden = forbiddenCommand(cmd, FORBIDDEN_CMDS);
|
||
if (forbidden?.uncertain) {
|
||
return { block: true, reason: "无法静态定位这条命令的共享资源操作;请通过 terminal_exec 的受管入口执行。" };
|
||
}
|
||
if (forbidden?.command) {
|
||
log("deny-bash-resource", cmd.slice(0, 500));
|
||
return { block: true, reason: `共享资源通过 bench stage run 调度:${forbidden.command}` };
|
||
}
|
||
return undefined;
|
||
}
|
||
const bad = badRedirect(cmd);
|
||
if (bad) {
|
||
log("deny-bash-redirect", cmd.slice(0, 500));
|
||
return { block: true, reason: `path-guard: 重定向到 workspace 之外(${bad})。写文件只在 workspace/(或 /tmp)。` };
|
||
}
|
||
const forbidden = forbiddenCommand(cmd, FORBIDDEN_CMDS);
|
||
if (forbidden?.command) {
|
||
log("deny-bash-cmd", cmd.slice(0, 500));
|
||
return { block: true, reason: `path-guard: 不要直接调用 ${forbidden.command}。编译/上板/评测走 bench stage run(它会选对 venv、拿资源池、落日志)。只读地 grep 这个词是允许的。` };
|
||
}
|
||
const badTarget = badWriteTarget(cmd);
|
||
if (badTarget) {
|
||
log("deny-bash-write", cmd.slice(0, 500));
|
||
return { block: true, reason: `path-guard: 写到 workspace 之外(${badTarget})。读白名单内的文件可以,写只能在 workspace/(或 /tmp)。` };
|
||
}
|
||
for (const rx of WRITE_PATTERNS) {
|
||
if (rx.test(cmd)) {
|
||
log("deny-bash", cmd.slice(0, 500));
|
||
return { block: true, reason: `path-guard: 命令被拒(${rx})。重阶段用 bench stage run,写文件只在 workspace/。` };
|
||
}
|
||
}
|
||
// 读边界:cd 禁用 + 逐 token 白名单判定(oracle / 兄弟 rollout / harness / state / ~/.pi / ~/toolchains …)
|
||
const v = guard.judgeCommand(cmd);
|
||
if (v) {
|
||
log("deny-bash-read", cmd.slice(0, 500));
|
||
return { block: true, reason: `path-guard: ${v.reason}。${HINT}` };
|
||
}
|
||
return undefined;
|
||
});
|
||
}
|