sync 2026-09-15_09:35:36 d-robotics@d-roboticsdeMacBook-Pro

This commit is contained in:
bench 2026-09-15 09:35:36 +08:00
parent 7ee7ca7c8f
commit 469a71dfd6
6 changed files with 321 additions and 51 deletions

View File

@ -3,12 +3,20 @@
你在一个受控的量化上板环境里工作。规则不是建议,违反会被工具层拒绝或被判负。
## 你能写的地方
只有当前目录(`workspace/`)。其他一切只读:`../job.json`、`data/`、`cache/`。越界写入会被 path-guard 拒绝并记录。
只有当前目录(`workspace/`)。其他一切只读。越界写入会被 path-guard 拒绝并记录。
目录关系(你的 cwd 是 `<job>/rollouts/<kk>/workspace/`):
- `..` = 本次 rollout(`agent/` `stages/` `handles/` `rollout.json`,可读)
- `../..` = `rollouts/`,里面是**别的 rollout,禁读**(每条轨迹必须独立作答)
- `../../..` = job 目录:`job.json`、`data/` 可读;`oracle/` 仅 repair 可读
- 任务提示里给的都是**绝对路径**,直接用绝对路径最省事。
- **命令里不要 `cd`/`pushd`**:path-guard 见到就拒(cwd 固定是 workspace,看别处用绝对路径)。glob(`../../ora*`)和 `find ../../..` 也按前缀判,绕不过去。
- 可读白名单之外的项目文件(`harness/` `state/` `export/` 别的 job)一律拒,工具链源码 `~/toolchains` 也拒——旋钮表就在下面。
## 该读什么、不该读什么(这决定你的预算够不够用)
- **先读** `../job.json`:任务、march、输入规格、四道门的阈值都在里面,读完就够了,不需要去读评判代码。
- **先读** `job.json`(提示里给了绝对路径):任务、march、输入规格、四道门的阈值都在里面,读完就够了,不需要去读评判代码。
- **不要读 `harness/` 和工具链的 site-packages 源码**。`harness/` 是评判你的代码,读它不会让你编译得更好;hmct / horizon_tc_ui 能改的旋钮下面全列了。第一批轨迹里有 agent 用 32 轮读源码、最后 3 轮才提交编译,预算见底——这是最差的用法。
- **e2e 任务看不到 `../../oracle/`**(会被拒)。你要自己走完;repair 任务会把 oracle 的失败现场直接给你。
- **e2e 任务看不到 `<job>/oracle/`**(会被拒)。你要自己走完;repair 任务会把 oracle 的失败现场直接给你。
- `.horizon/skills/` 只挑与你任务直接相关的 1–2 个 skill 读,读完就动手。
## compile.yaml 里你能改的旋钮(PTQ · hb_compile 3.5.3 · hmct 2.6.5,已对源码核实)
@ -21,7 +29,8 @@ input_parameters: # 默认 featuremap/NCHW/无归一化,预处理在
input_layout_train: NCHW
# mean_value / scale_value / std_value 仅当 input_type 不是 featuremap 时有意义
calibration_parameters:
calibration_type: default # default | max | kl | mix | skip ← 首选旋钮。default = 自动搜索;掉点大时试 kl 或 mix
calibration_type: default # default | max | kl | mix ← 首选旋钮。default = 自动搜索;掉点大时试 kl 或 mix
# (还有 skip = 完全不校准,只给预量化模型用;普通 PTQ 选它必崩 G2/G3,白烧一次编译)
max_percentile: 0.99999 # 只对 max 有效,截掉激活长尾(0.9999–1.0)
per_channel: true # 权重逐通道量化,对深度可分离卷积(MobileNet/EfficientNet)常有帮助
run_on_cpu: "node_a,node_b" # 把敏感节点放 CPU(float),逗号分隔 ONNX 节点名;run_on_bpu 反之
@ -31,8 +40,8 @@ calibration_parameters:
op_config: { Softmax: { qtype: float16 }, LayerNormalization: { qtype: float16 } } # 按算子类型
node_config: { "/features/features.8/features.8.2/Mul": { qtype: int16 } } # 按节点名(看 hb_compile 日志里的逐节点余弦表挑最差的)
compiler_parameters:
compile_mode: latency # latency | bandwidth
optimize_level: O2 # O0 | O1 | O2 | O3(O3 编译更久)
compile_mode: latency # latency | bandwidth | balance
optimize_level: O2 # O0 | O1 | O2 —— hbdk4 后端只接受这三个,写 O3 会在 yaml 校验阶段直接报错
jobs: 8
```

View File

@ -1 +1 @@
2
3

View File

@ -0,0 +1,129 @@
/**
* guard-core — path-guard 的判定核心(纯函数,无 pi 依赖,node 可直接单测:node agent/extensions/guard-core.test.js)。
*
* 读白名单,不是黑名单正则:agent 能碰的路径只有
* workspace/(可写)· 本 rollout 的 agent/ stages/ handles/ rollout.json(只读)
* ../../job.json · ../../data(→ bench/data)· bench/cache · bench/data · bench/bin · .horizon/ · AGENTS.md
* repair 模式额外:../../oracle/
* 项目树(PROJECT_ROOT 下)里其余一切拒:别的 rollout(GRPO 组内独立性)、oracle(e2e 抄答案)、harness/(评判代码)、
* state/(全部 job 的 sqlite)、export/ datasets/ reports/ .env*。
* 项目树之外:~/.pi(API key)、~/toolchains(工具链源码,旋钮表已在系统提示里)拒;其他系统路径放行。
*
* bash:命令里含 cd/pushd/popd 直接拒(cwd 就是 workspace,看别处用绝对路径;cd 是所有绕过的根);
* 把命令里像路径的 token 全抠出来逐个判(相对路径按 cwd=workspace 解析,glob 在第一个 *?[ 处截断取前缀)。
*/
"use strict";
const path = require("node:path");
const fs = require("node:fs");
function norm(p) {
return path.resolve(p).replace(/\/+$/, "");
}
function under(p, root) {
return p === root || p.startsWith(root + path.sep);
}
/**
* @param {object} env { WORKSPACE, ROLLOUT, JOB_DIR, BENCH_ROOT, PROJECT_ROOT, HOME, MODE, DENY_EXTRA?, ALLOW_EXTRA? }
* ALLOW_EXTRA:优先于一切拒绝的放行前缀——本 rollout 在 work_root 下的编译工作目录(work_root/<run>/<job>/r<kk>)。
* DENY_EXTRA:项目树之外也要拒的绝对路径数组——state/ 与 work_root 在机器上是指向 /home 的符号链接,
* 直接用真实路径就绕过了"项目树内白名单",所以 harness 把它们的 realpath 通过 BENCH_GUARD_DENY 传进来。
*/
function makeGuard(env) {
const WS = norm(env.WORKSPACE);
const RO = norm(env.ROLLOUT);
const JOB = norm(env.JOB_DIR);
const BENCH = norm(env.BENCH_ROOT);
const PROJ = norm(env.PROJECT_ROOT);
const HOME = norm(env.HOME || "/nonexistent-home");
const MODE = env.MODE || "e2e";
// 从 workspace/ 看:.. = 本 rollout(agent/ stages/ handles/ rollout.json)· ../.. = rollouts/(兄弟,禁)· ../../.. = job 目录(job.json data/ oracle/)
const allowRead = [
WS, RO,
path.join(JOB, "job.json"), path.join(JOB, "data"),
path.join(BENCH, "data"), path.join(BENCH, "cache"), path.join(BENCH, "bin"), path.join(BENCH, "agent", "oe-doc.sh"),
path.join(PROJ, ".horizon"), path.join(PROJ, "AGENTS.md"),
];
if (MODE === "repair") allowRead.push(path.join(JOB, "oracle"));
const allowExtra = (env.ALLOW_EXTRA || []).filter(Boolean).map(norm);
// 明确拒(即使不在项目树内)
const denyAlways = [path.join(HOME, ".pi"), path.join(HOME, "toolchains"), path.join(BENCH, ".env.local"),
...(env.DENY_EXTRA || []).filter(Boolean).map(norm)];
// 白名单目录的真实路径(data/ cache/ 是符号链接):agent 用 realpath 访问也放行
const allowReal = [];
for (const a of allowRead) { try { const r = fs.realpathSync(a); if (r !== a) allowReal.push(r); } catch {} }
// 项目树内的"敏感"名字,报错信息用
const why = (p) => {
if (under(p, path.join(JOB, "oracle"))) return "e2e 不能看 oracle/(标准答案)";
if (under(p, path.join(JOB, "rollouts")) && !under(p, RO)) return "不能看别的 rollout(轨迹必须独立)";
if (under(p, path.join(BENCH, "harness")) || under(p, path.join(BENCH, "export"))) return "harness/ 是评判代码,读它不会让你编译得更好";
if (under(p, path.join(BENCH, "state"))) return "state/ 是全部 job 的状态库";
for (const d of (env.DENY_EXTRA || [])) if (d && under(p, norm(d))) return "harness 的工作目录/状态库,不是你的";
if (under(p, path.join(HOME, ".pi"))) return "~/.pi 含凭证";
if (under(p, path.join(HOME, "toolchains"))) return "工具链源码不用读,可改的旋钮都在系统提示里";
return "不在可读白名单里";
};
/** 解析一个路径 token → 绝对路径(去引号、去 glob 尾巴、展开 ~),空则 null */
function resolveToken(tok) {
let t = tok.replace(/^['"`]+|['"`]+$/g, "");
if (!t) return null;
if (t.startsWith("~")) t = HOME + t.slice(1);
const g = t.search(/[*?[]/);
if (g >= 0) t = t.slice(0, g) || ".";
if (t.endsWith("/") && t.length > 1) t = t.slice(0, -1);
return norm(path.isAbsolute(t) ? t : path.join(WS, t));
}
/** @returns {null | {block:true, reason:string, path:string}} */
function judgePath(tok, { write = false } = {}) {
const p = resolveToken(tok);
if (!p) return null;
if (write) {
return under(p, WS) ? null : { block: true, reason: `只能写 workspace/ 内(${tok})`, path: p };
}
for (const a of allowExtra) if (under(p, a)) return null;
for (const d of denyAlways) if (under(p, d)) return { block: true, reason: `${why(p)}(${tok})`, path: p };
if (under(p, PROJ)) {
for (const a of allowRead) if (under(p, a)) return null;
// 不放行白名单目录的祖先本身:ls ../.. 会列出 oracle/ rollouts/ —— 用不到
return { block: true, reason: `${why(p)}(${tok})`, path: p };
}
for (const a of allowReal) if (under(p, a)) return null;
// 项目树外:若是符号链接指回项目树(或 realpath 命中 denyAlways),按真实路径再判一次
let real = null;
try { real = fs.realpathSync(p); } catch {}
if (real && real !== p) {
for (const d of denyAlways) if (under(real, d)) return { block: true, reason: `${why(real)}(${tok} → ${real})`, path: real };
if (under(real, PROJ)) {
for (const a of allowRead) if (under(real, a)) return null;
return { block: true, reason: `${why(real)}(${tok} → ${real})`, path: real };
}
}
return null; // 项目树外的系统路径(/usr /tmp /dev …)
}
const CD_RX = /(^|[;&|(]\s*|\bthen\s+|\bdo\s+|\belse\s+)\s*(cd|pushd|popd)(\s|$|;|&|\|)/;
// 像路径的 token:含 / ,或以 . ~ 开头(含 .. .env),排除 URL、选项开头、数字
const TOKEN_RX = /(?:^|[\s=:(){}'"`,;|&<>])((?:~|\.{1,2}|\/)[^\s'"`;|&<>(){}]*|[A-Za-z0-9_.\-@+]+\/[^\s'"`;|&<>(){}]*)/g;
function judgeCommand(cmd) {
if (CD_RX.test(cmd)) return { block: true, reason: "命令里不要用 cd/pushd/popd:cwd 就是 workspace,看别处请用绝对路径", path: "cd" };
let m;
TOKEN_RX.lastIndex = 0;
while ((m = TOKEN_RX.exec(cmd)) !== null) {
const tok = m[1];
if (/^(https?|ssh|git|file):\/\//.test(tok)) continue;
if (/^\d+(\.\d+)*\/?$/.test(tok)) continue; // 版本号之类
if (tok === "/" || tok === "." ) continue;
const v = judgePath(tok);
if (v) return v;
}
return null;
}
return { judgePath, judgeCommand, resolveToken, allowRead };
}
module.exports = { makeGuard };

View File

@ -0,0 +1,85 @@
// node agent/extensions/guard-core.test.js —— path-guard 判定核心的单测(含另一个会话报的 5 个绕过 + 兄弟 rollout 泄漏)
"use strict";
const { makeGuard } = require("./guard-core.js");
const P = "/mnt/data/u/proj";
const B = P + "/bench";
const JOB = B + "/runs/r1/cls--m--nash-e--ptq--int8";
const RO = JOB + "/rollouts/01";
const WS = RO + "/workspace";
const base = { WORKSPACE: WS, ROLLOUT: RO, JOB_DIR: JOB, BENCH_ROOT: B, PROJECT_ROOT: P, HOME: "/home/u",
DENY_EXTRA: ["/home/u/bench-state", "/home/u/bench-work"],
ALLOW_EXTRA: ["/home/u/bench-work/r1/cls--m--nash-e--ptq--int8/r01"] };
const e2e = makeGuard({ ...base, MODE: "e2e" });
const repair = makeGuard({ ...base, MODE: "repair" });
let fails = 0;
function expect(name, got, want) {
const ok = (got === null) === (want === "allow");
if (!ok) { fails++; console.log(`FAIL ${name}: want ${want}, got ${got ? "block(" + got.reason + ")" : "allow"}`); }
else console.log(`ok ${name} → ${want}`);
}
// ---- read tool paths
expect("read job.json", e2e.judgePath("../../../job.json"), "allow");
expect("read own agent/prompt", e2e.judgePath("../agent/prompt.md"), "allow");
expect("read own stages log", e2e.judgePath(RO + "/stages/05-compile/a00/stderr.log"), "allow");
expect("read data", e2e.judgePath("../../../data/calib/cls/1x3x224x224/manifest.json"), "allow");
expect("read cache onnx", e2e.judgePath(B + "/cache/onnx/abc/m.onnx"), "allow");
expect("read horizon skill", e2e.judgePath(P + "/.horizon/skills/hmct/SKILL.md"), "allow");
expect("read oracle (e2e)", e2e.judgePath("../../../oracle/result.json"), "block");
expect("read oracle (repair)", repair.judgePath("../../../oracle/result.json"), "allow");
expect("read sibling claim", e2e.judgePath("../../00/claim.json"), "block");
expect("read sibling via rollouts/", e2e.judgePath("../../00/workspace/compile.yaml"), "block");
expect("read harness", e2e.judgePath(B + "/harness/gates.py"), "block");
expect("read state sqlite", e2e.judgePath(B + "/state/jobs.sqlite"), "block");
expect("read ~/.pi", e2e.judgePath("~/.pi/agent/models.json"), "block");
expect("read toolchain src", e2e.judgePath("/home/u/toolchains/oe-3.7.0/venv/lib/python3.10/site-packages/hmct/x.py"), "block");
expect("read /usr/lib", e2e.judgePath("/usr/lib/python3.10/json/__init__.py"), "allow");
expect("read job dir itself", e2e.judgePath("../../.."), "block");
expect("read env.local", e2e.judgePath(B + "/.env.local"), "block");
expect("read state realpath", e2e.judgePath("/home/u/bench-state/jobs.sqlite"), "block");
expect("read own work dir", e2e.judgePath("/home/u/bench-work/r1/cls--m--nash-e--ptq--int8/r01/05-compile-work/x.bc"), "allow");
expect("read sibling work dir", e2e.judgePath("/home/u/bench-work/r1/cls--m--nash-e--ptq--int8/r00/05-compile-work/x.bc"), "block");
expect("read work_root", e2e.judgePath("/home/u/bench-work/r1/job/oracle/04-calibrate/x.yaml"), "block");
expect("read data realpath (not in allowReal on test box → outside proj → allow)", e2e.judgePath("/home/u/bench-data/calib/x.npy"), "allow");
// ---- write tool
expect("write ws", e2e.judgePath("compile.yaml", { write: true }), "allow");
expect("write ws abs", e2e.judgePath(WS + "/x/y.yaml", { write: true }), "allow");
expect("write agent dir", e2e.judgePath("../agent/x", { write: true }), "block");
expect("write job.json", e2e.judgePath("../../../job.json", { write: true }), "block");
// ---- bash(另一个会话实测的 5 个绕过)
expect("cat ../../oracle", e2e.judgeCommand("cat ../../../oracle/result.json"), "block");
expect("python open oracle", e2e.judgeCommand("python3 -c 'print(open(\"../../../oracle/result.json\").read())'"), "block");
expect("cd && cat oracle", e2e.judgeCommand("cd ../../.. && cat oracle/result.json"), "block");
expect("cd;cd;head oracle", e2e.judgeCommand("cd .. ; cd .. ; cd .. ; head oracle/stages/04-calibrate/compile.yaml"), "block");
expect("ls ../../../ora*", e2e.judgeCommand("ls ../../../ora*"), "block");
expect("find .. -exec cat (own rollout only)", e2e.judgeCommand("find .. -name result.json -exec cat {} +"), "allow");
expect("find ../.. -exec cat", e2e.judgeCommand("find ../../.. -name result.json -exec cat {} +"), "block");
expect("cat sibling claim", e2e.judgeCommand("cat ../../02/claim.json"), "block");
expect("ls sibling ws", e2e.judgeCommand("ls ../../00/workspace"), "block");
expect("cd only", e2e.judgeCommand("cd .. && ls"), "block");
expect("pushd", e2e.judgeCommand("pushd ../.. && ls"), "block");
expect("cd in subshell", e2e.judgeCommand("(cd ../..; ls)"), "block");
expect("word containing cd", e2e.judgeCommand("echo abcd/x.txt > note.txt"), "allow");
// ---- bash 合法用法
expect("bench stage run", e2e.judgeCommand("bench stage run compile --config workspace/compile.yaml"), "allow");
expect("bench stage run abs", e2e.judgeCommand("bench stage run compile --config " + WS + "/compile.yaml"), "allow");
expect("cat job.json", e2e.judgeCommand("cat ../../../job.json | head -50"), "allow");
expect("tail own stage log", e2e.judgeCommand("tail -50 ../stages/05-compile/a00/stderr.log 2>/dev/null"), "allow");
expect("ls skills", e2e.judgeCommand("ls " + P + "/.horizon/skills/"), "allow");
expect("python one-liner no path", e2e.judgeCommand("python3 -c 'import yaml; print(yaml.safe_load(open(\"compile.yaml\")))'"), "allow");
expect("redirect to /dev/null", e2e.judgeCommand("bench stage wait h --poll 60 2>/dev/null"), "allow");
expect("version-like token", e2e.judgeCommand("echo 3.5.3/hbdk 4.7.5"), "allow");
expect("url", e2e.judgeCommand("echo https://x.y/z"), "allow");
expect("read own rollout.json", e2e.judgeCommand("cat ../rollout.json"), "allow");
expect("ls ..(own rollout)", e2e.judgeCommand("ls .."), "allow");
expect("ls ../.. (rollouts dir)", e2e.judgeCommand("ls ../.."), "block");
expect("repair reads oracle yaml", repair.judgeCommand("cp ../../../oracle/stages/04-calibrate/compile.yaml ./compile.yaml"), "allow");
console.log(fails ? `\n${fails} FAILED` : "\nall passed");
process.exit(fails ? 1 : 0);

View File

@ -1,23 +1,45 @@
/**
* path-guard — agent 只有一个可写区:<rollout>/workspace/。
* path-guard — agent 的读写边界(判定核心在 guard-core.js,纯函数,node 可单测:node agent/extensions/guard-core.test.js)。
*
* 拦截 write / edit 的目标路径;拦截 bash 里明显的越界写入(重定向、cp/mv/rm/tee/chmod 指向 workspace 之外)。
* bash 的静态检查不可能完备,第二道保险是文件系统权限:harness 把 job.json / data / cache / eval 设为只读。
* 每次拒绝写一行到 <rollout>/agent/guard.log,L4 导出时可统计"越界尝试次数"。
* 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`。
*
* API 形状对照 pi-coding-agent 的 ExtensionAPI(P3 按安装版本核对:pi.on("tool_call") 返回 { block, reason })。
* 白名单(e2e):workspace/(可写)· 本 rollout 根(agent/ stages/ handles/ rollout.json)· <job>/job.json · <job>/data
* · bench/data · bench/cache · bench/bin · bench/agent/oe-doc.sh · <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 } = _require("./guard-core.js") as { makeGuard: (env: Record<string, string>) => any };
const ROLLOUT = process.env.BENCH_ROLLOUT_DIR ?? "";
const MODE = process.env.BENCH_MODE ?? "e2e";
// e2e 任务看不到标准答案(oracle/),否则轨迹只是抄答案;repair 任务的输入就是 oracle 的失败现场,放行
const ORACLE_RX = /(^|[\s/"'=])(\.\.\/)*\.\.\/\.\.\/oracle(\/|\b)|\/oracle\//;
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, "agent", "guard.log") : "";
// harness/rollout.py agent_env 传入:BENCH_GUARD_DENY = realpath(state):work_root(都是指向 /home 的链接/本地盘,绕开项目树白名单);
// BENCH_GUARD_ALLOW = 本 rollout 在 work_root 下的编译工作目录。
const splitEnv = (k: string) => (process.env[k] ?? "").split(":").filter(Boolean);
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: splitEnv("BENCH_GUARD_DENY"), ALLOW_EXTRA: splitEnv("BENCH_GUARD_ALLOW"),
});
function inside(p: string): boolean {
const abs = path.resolve(WORKSPACE, p);
return abs === WORKSPACE || abs.startsWith(WORKSPACE + path.sep);
@ -31,10 +53,8 @@ function log(kind: string, detail: string) {
} catch { /* 日志失败不影响拒绝 */ }
}
// bash 静态规则:命中即拒。故意保守——误杀一个合法命令的代价远小于放过一次越界写。
// 第一版把 `2>/dev/null` 也当成了"重定向到绝对路径",第一条真实轨迹里 10 次调用被拒 4 次,全是误杀 → 重定向单独判目标。
// 被禁的**命令**:只在"作为命令被调用"的位置匹配(行首 / ; | && 之后 / env 赋值之后),
// 否则 `ps aux | grep hb_compile`、`grep -r hb_compile logs/` 这类只读排查也会被拒——第一条轨迹里真的被拒了。
// ---- bash 静态规则(v1 保留)。故意保守——误杀一个合法命令的代价远小于放过一次越界。
// 被禁的**命令**只在"作为命令被调用"的位置匹配,`grep -r hb_compile logs/` 这类只读排查不拒。
const FORBIDDEN_CMDS = ["hb_compile", "hb_mapper", "hb_verifier", "hrt_model_exec", "hb_perf",
"ssh", "scp", "rsync", "wget", "curl", "pip", "pip3", "conda", "npm"];
const CMD_POSITION = new RegExp(
@ -45,8 +65,7 @@ const WRITE_PATTERNS: RegExp[] = [
/\bgit\s+push\b/,
];
// 写文件的命令:destLast 的最后一个参数才是目的地(cp ../job.json . 是合法的"读进来");
// 其余命令的每个非选项参数都是目标。/tmp 放行(scratch),/dev/null 放行。
// 写文件的命令: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"]);
@ -73,7 +92,7 @@ function badWriteTarget(cmd: string): string | null {
}
const REDIRECT_OK = new Set(["/dev/null", "/dev/stderr", "/dev/stdout"]);
/** 找出所有输出重定向目标(> >> 2> &>),有一个落在 workspace 外就拒。相对路径以 cwd=workspace 解析;`cd` 过的命令无法追踪,靠文件权限兜底。 */
/** 所有输出重定向目标(> >> 2> &>),有一个落在 workspace 外就拒。cd 已被整体禁用,所以相对路径按 cwd=workspace 解析是可靠的。 */
function badRedirect(cmd: string): string | null {
const re = /(?:^|[\s;|&(])(?:\d?>>?|&>)\s*['"]?([^\s'"|;&)]+)/g;
let m: RegExpExecArray | null;
@ -82,53 +101,69 @@ function badRedirect(cmd: string): string | null {
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 = "自己走:写 workspace/compile.yaml → bench stage run compile;任务提示里的路径都是绝对路径,直接用。";
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 ?? {};
if (MODE !== "repair") {
const target: string = name === "bash" ? (input.command ?? "") : (input.path ?? input.file_path ?? "");
if (ORACLE_RX.test(target)) {
log("deny-oracle", target.slice(0, 300));
return { block: true, reason: "path-guard: e2e 任务不能看 oracle/(标准答案)。自己走:写 workspace/compile.yaml → bench stage run compile。" };
// 文件类工具: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 ?? "";
if (!p || !inside(p)) {
const v = p ? guard.judgePath(p, { write: true }) : { reason: "缺少 path" };
if (v) {
log("deny-" + name, p);
return { block: true, reason: `path-guard: 只能写 workspace/ 内的文件(${p})` };
return { block: true, reason: `path-guard: ${v.reason}` };
}
return undefined;
}
if (name !== "bash") return undefined;
const cmd: string = input.command ?? "";
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 = CMD_POSITION.exec(cmd);
if (forbidden) {
log("deny-bash-cmd", cmd.slice(0, 500));
return { block: true, reason: `path-guard: 不要直接调用 ${forbidden[1]}。编译/上板/评测走 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/。` };
}
}
if (name === "bash") {
const cmd: string = input.command ?? "";
const bad = badRedirect(cmd);
if (bad) {
log("deny-bash-redirect", cmd.slice(0, 500));
return { block: true, reason: `path-guard: 重定向到 workspace 之外(${bad})。写文件只在 workspace/。` };
}
const forbidden = CMD_POSITION.exec(cmd);
if (forbidden) {
log("deny-bash-cmd", cmd.slice(0, 500));
return { block: true, reason: `path-guard: 不要直接调用 ${forbidden[1]}。编译/上板/评测走 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;
});

View File

@ -141,7 +141,19 @@ def agent_env(rd: Path, mode: str = "e2e") -> dict:
if cfg["paths"].get("node_bin"):
path_parts.append(str(Path(paths.expand(cfg["paths"]["node_bin"])).parent))
path_parts.append(os.environ.get("PATH", "/usr/bin:/bin"))
# path-guard 的白名单按"项目树内路径"判;state/ 是指向 /home 的符号链接、work_root 是本地盘,
# 用真实路径就绕开了 → 把它们的 realpath 作为额外拒绝项传给 guard;本 rollout 自己的编译工作目录放行。
work_root = cfg["paths"].get("work_root")
deny = [str(Path(paths.STATE).resolve())]
allow = []
if work_root:
wr = paths.expand(work_root)
deny.append(str(wr))
rel = rd.relative_to(paths.RUNS) # <run>/<job>/rollouts/<kk>
allow.append(str(wr / rel.parts[0] / rel.parts[1] / f"r{int(rel.parts[3]):02d}"))
return {
"BENCH_GUARD_DENY": ":".join(deny),
"BENCH_GUARD_ALLOW": ":".join(allow),
"PATH": ":".join(path_parts),
"HOME": os.environ.get("HOME", "/tmp"),
"LANG": "C.UTF-8",