Download policies/_lib/state.mjs from failproofai/fire-runtime-policy-reliability: direct link, hf CLI and curl.
- Browser
- Download file 6.51 kB
-
https://huggingface.co/datasets/failproofai/fire-runtime-policy-reliability/resolve/main/policies/_lib/state.mjs
- Command line
-
hf download hf://datasets/failproofai/fire-runtime-policy-reliability/policies/_lib/state.mjs
-
curl -L -o state.mjs https://huggingface.co/datasets/failproofai/fire-runtime-policy-reliability/resolve/main/policies/_lib/state.mjs
6.51 kB
| // Shared helpers for the study's policies. | |
| // | |
| // FailproofAI runs each policy as a SUBPROCESS per tool call, so nothing is | |
| // retained in memory between calls. Any counter that must survive across the | |
| // run has to live on disk. State is keyed per working directory so concurrent | |
| // trials on the same host never share counters -- Harbor runs many tasks in | |
| // parallel, and cross-contaminated state would silently couple them. | |
| import { readFileSync, writeFileSync, mkdirSync } from "node:fs"; | |
| import { join } from "node:path"; | |
| import { tmpdir } from "node:os"; | |
| import { createHash } from "node:crypto"; | |
| const dir = join(tmpdir(), "failproof-study"); | |
| // Key on the session, NOT the directory. `ctx.cwd` does not exist -- the hook | |
| // context carries `ctx.session.{sessionId,cwd}` (verified by dumping ctx from a | |
| // real hook invocation), so keying on `ctx?.cwd` silently fell through to the | |
| // hook process's own cwd. Codex spawns the hook from the tool's workdir, so a | |
| // task that worked in two directories got two independent sets of counters and | |
| // nudged twice for what the design calls "at most twice per run" -- observed as | |
| // 2 verify-depth nudges in one trial, where the cap is 1. | |
| // | |
| // sessionId is per agent run, which is the unit the caps are written against, | |
| // and it is distinct across concurrent Harbor trials. | |
| function pathFor(ctx) { | |
| const id = ctx?.session?.sessionId || ctx?.session?.cwd || process.cwd(); | |
| const key = createHash("sha1").update(String(id)).digest("hex").slice(0, 16); | |
| return join(dir, `${key}.json`); | |
| } | |
| export function load(ctx) { | |
| try { | |
| return JSON.parse(readFileSync(pathFor(ctx), "utf8")); | |
| } catch { | |
| return { writes: 0, checks: 0, reads: {}, nudges: {}, cmds: 0 }; | |
| } | |
| } | |
| export function save(ctx, s) { | |
| try { | |
| mkdirSync(dir, { recursive: true }); | |
| writeFileSync(pathFor(ctx), JSON.stringify(s)); | |
| } catch { | |
| /* never let bookkeeping break the agent's run */ | |
| } | |
| } | |
| // Extract a shell command from whatever shape the harness passes. Codex sends | |
| // exec_command payloads; Claude Code sends a plain `command` field. | |
| export function commandOf(ctx) { | |
| const i = ctx?.toolInput || {}; | |
| if (typeof i.command === "string") return i.command; | |
| if (Array.isArray(i.command)) return i.command.join(" "); | |
| if (typeof i.cmd === "string") return i.cmd; | |
| const blob = typeof i === "string" | |
| ? i | |
| : typeof i.input === "string" | |
| ? i.input | |
| : JSON.stringify(i || {}); | |
| // Code-mode wraps exec_command in JavaScript, so the key is commonly an | |
| // unquoted object-literal key (`cmd:"..."`) inside toolInput.input. Older | |
| // harnesses serialize it as JSON (`"cmd":"..."`). Accept both shapes. | |
| const m = blob.match(/(?:"cmd"|\bcmd)\s*:\s*"((?:[^"\\]|\\.)*)"/); | |
| if (m) { try { return JSON.parse(`"${m[1]}"`); } catch { return m[1]; } } | |
| return ""; | |
| } | |
| // UserPromptSubmit has changed shape across supported CLIs. Keep prompt | |
| // detection defensive so policies can decide whether they are relevant without | |
| // firing on every task. This reads only the user's task text; no task names or | |
| // fixtures are embedded in policy source. | |
| export function promptOf(ctx) { | |
| for (const value of [ | |
| ctx?.prompt, | |
| ctx?.userPrompt, | |
| ctx?.message, | |
| ctx?.input, | |
| ctx?.payload?.prompt, | |
| ctx?.payload?.message, | |
| ctx?.payload?.content, | |
| ctx?.toolInput?.prompt, | |
| ctx?.toolInput?.message, | |
| ctx?.toolInput?.content, | |
| ]) { | |
| if (typeof value === "string") return value; | |
| } | |
| return ""; | |
| } | |
| // A command that verifies work, as opposed to producing it. | |
| export const CHECK = | |
| /\b(pytest|py\.test|unittest|npm\s+(?:run\s+)?test|yarn\s+test|go\s+test|cargo\s+test|make\s+(?:test|check)|tox|jest|mocha|rspec|bats|shellcheck|mypy|ruff|flake8|diff|cmp|sha256sum|md5sum|grep\s+-q)\b/i; | |
| export const WRITE = | |
| /(apply_patch|\bAdd File:|\bUpdate File:|>>?\s|\btee\b|\bmkdir\b|\btouch\b|\bcp\b|\bmv\b)/; | |
| export const READ = /\b(?:cat|head|tail|less|nl|bat|sed\s+-n)\s+(\S+)/; | |
| // Anything that EXERCISES the work, which is broader than CHECK. The host-2 | |
| // baseline showed failures run test-runner commands slightly MORE often than | |
| // passes (46% vs 39%), so "did it run pytest" is the wrong question. "Did it | |
| // execute what it just built and look at the output" is the one that matters: | |
| // measured behaviourally, 89% of failures executed something, and the single | |
| // trial that did neither is the only run that truly checked nothing. | |
| export const EXEC = | |
| /(^|[|;&]\s*)(python3?|node|bash|sh|zsh|java|go\s+run|cargo\s+run|Rscript|perl|ruby|make\b|\.\/\S+)|\b(pytest|py\.test|unittest|npm\s+(?:run\s+)?test|jest|mocha|curl\s)/i; | |
| // The file a write-ish command touched, so guidance can name the agent's own | |
| // artifact instead of asking abstractly for "verification". Task-agnostic by | |
| // construction: the path comes from what the agent did, never from the task. | |
| const PATCH_PATH = /\*\*\*\s+(?:Add|Update)\s+File:\s+(\S+)/; | |
| const REDIRECT_PATH = />>?\s*(\S+)/; | |
| const TOUCH_PATH = /\b(?:tee|touch|cp|mv)\s+(?:-\S+\s+)*(?:\S+\s+)?(\S+)/; | |
| export function writtenPath(cmd) { | |
| for (const re of [PATCH_PATH, REDIRECT_PATH, TOUCH_PATH]) { | |
| const m = re.exec(cmd); | |
| if (m && m[1] && !m[1].startsWith("-")) return m[1]; | |
| } | |
| return ""; | |
| } | |
| // Ordering, not counts. A Stop gate needs to know whether anything ran AFTER | |
| // the last edit -- an agent that edits, verifies, then edits again has left its | |
| // final state unexercised even though it "did verify" at some point. | |
| export function noteCommand(s, cmd) { | |
| s.seq = (s.seq || 0) + 1; | |
| if (WRITE.test(cmd)) { | |
| s.lastWrite = s.seq; | |
| const p = writtenPath(cmd); | |
| if (p) s.lastWritePath = p; | |
| } | |
| if (EXEC.test(cmd)) s.lastExec = s.seq; | |
| return s; | |
| } | |
| // True when the agent's most recent edit was never exercised afterwards. | |
| export function endsUnverified(s) { | |
| if (!s.lastWrite) return false; // nothing written: nothing to exercise | |
| return !s.lastExec || s.lastExec < s.lastWrite; | |
| } | |
| // Fire a given nudge at most `max` times per run. Repeating the same guidance | |
| // wastes context and trains the agent to ignore it. | |
| export function shouldNudge(s, key, max = 2) { | |
| // A state file written before `nudges` existed -- or any partial write -- | |
| // would make this throw, and a throw inside a hook takes the policy layer | |
| // down mid-trial. Bookkeeping must never be able to break the agent's run. | |
| if (!s.nudges || typeof s.nudges !== "object") s.nudges = {}; | |
| const n = s.nudges[key] || 0; | |
| if (n >= max) return false; | |
| s.nudges[key] = n + 1; | |
| return true; | |
| } | |