File size: 3,039 Bytes
105e2f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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.`
    );
  },
});