// Narrow threshold policy for reverse-engineered replacement programs. It // avoids exposing ordinary threshold-bearing tasks to a generic optimization // nudge. import { customPolicies, allow, instruct } from "failproofai"; import { load, save, commandOf, promptOf, shouldNudge } from "../_lib/state.mjs"; const REIMPLEMENT = /\b(?:compiled|existing)\s+(?:an?\s+|the\s+)?(?:program|binary|executable)\b/i; const EQUIVALENCE = /\b(?:identical|equivalent|same)\s+(?:operation|output|behavio[u]?r)\b/i; const INDEPENDENT = /\b(?:independent|isolation|must not invoke|without (?:calling|running|using))\b/i; const SIZE_BAR = /(?:<|under|at most|no more than)\s*\d+\s*(?:k|kb|bytes?)\b/i; const MEASURE = /\b(?:cmp|diff|sha256sum|gzip\s*\|\s*wc|wc\s+-c)\b/i; const CANDIDATE_BUILD = /\bgcc\b[^\n]{0,240}\b(?:mystery\.c|reversed)\b|\.\/reversed\b/i; customPolicies.add({ name: "final-binary-equivalence-track", match: { events: ["UserPromptSubmit", "PreToolUse"] }, fn: async (ctx) => { const s = load(ctx); const prompt = promptOf(ctx); if (prompt && REIMPLEMENT.test(prompt) && EQUIVALENCE.test(prompt) && INDEPENDENT.test(prompt) && SIZE_BAR.test(prompt)) { s.finalBinaryEquivalence = true; } const cmd = commandOf(ctx); if (cmd && MEASURE.test(cmd)) s.finalBinaryMeasurement = true; save(ctx, s); return allow(); }, }); customPolicies.add({ name: "final-binary-equivalence-before-candidate-run", match: { events: ["PreToolUse"] }, fn: async (ctx) => { const s = load(ctx); const cmd = commandOf(ctx); if (!s.finalBinaryEquivalence || !cmd || !CANDIDATE_BUILD.test(cmd)) return allow(); if (!shouldNudge(s, "final-binary-before-run", 1)) return allow(); save(ctx, s); return instruct( `The first candidate must be measured against the original immediately, before further approximation. ` + `Run both in isolated directories with the evaluator-facing build and invocation, compare every output ` + `artifact and stream byte-for-byte, and record the actual similarity or mismatch. If equivalence fails, ` + `switch to reverse-engineering the missing operation instead of polishing a visually similar renderer.` ); }, }); customPolicies.add({ name: "final-binary-equivalence", match: { events: ["Stop"] }, fn: async (ctx) => { const s = load(ctx); if (!s.finalBinaryEquivalence || !shouldNudge(s, "final-binary-equivalence", 1)) return allow(); save(ctx, s); return instruct( `Before stopping, validate the replacement program against the original over a diverse input set using ` + `the exact build and invocation contract. Record output differences and the compressed source size, and ` + `verify that the replacement remains independent when the original executable is unavailable. If any ` + `measured acceptance bar is missed, change the implementation method rather than declaring approximate ` + `behavior sufficient. Preserve the best measured artifact.` ); }, });