interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / research / om-bench / bench / leakcheck.py

132 lines · 6283 bytes Raw Blame History
1#!/usr/bin/env python3
2"""om-bench (#653) near-duplicate and renamed-leak check across splits (disclosed host tool).
3
4Statements are normalised (lower case, every number -> N, a synonym map folds common rewordings,
5punctuation and stop words dropped). Three similarities per cross-family pair: word-set Jaccard,
6content-word overlap coefficient |A&B|/min(|A|,|B|), and character 5-gram Jaccard. A pair is FLAGGED
7by rule(): word >= 0.6, or char5 >= 0.6, or (overlap >= 0.9 and word >= 0.45). Flagged pairs are a
8screening list: each is reviewed by hand and recorded in leak-reviews.json with a verdict (duplicate /
9template / same-split-overlap). Exit 1 if a flag has no review or a cross-split pair is a duplicate. Same-family pairs (variants of one family, always one
10split) are expected to be similar and are not flagged.
11
12Planted controls: PLANT1 (a dev statement reworded) MUST be flagged, otherwise exit 1. The v1 normaliser
13(no stop words, no overlap coefficient; word or char5 >= 0.6) missed PLANT1 (0.54 word); v2 was tuned
14after seeing PLANT1, so PLANT1 is not a blind control. A pure overlap>=0.6 rule flagged 807 pairs and was
15rejected as useless for review. PLANT2 (a heavy paraphrase of a train statement) is reported, not required:
16it measures the limit of a lexical check. Sealed task ids are printed only as keyed opaque ids
17(benchlib.opaque: HMAC-SHA256 under a private key, so family names cannot be recovered by guessing).
18Limits: lexical only; a leak paraphrased beyond the synonym map, or one that reuses a solution rather
19than a statement, is not detected.
20"""
21import itertools, json, os, re, sys
22sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
23from benchlib import opaque
24
25THRESH = 0.6
26SYN = {"output": "print", "write": "print", "display": "print", "emit": "print", "integer": "number",
27 "integers": "numbers", "value": "number", "values": "numbers", "given": "read", "input": "read",
28 "obtain": "read", "count": "number", "amount": "number", "string": "line", "text": "line",
29 "whether": "if", "otherwise": "else", "below": "under", "less": "under", "parentheses": "bracket",
30 "brackets": "bracket", "parenthesis": "bracket", "possibly": "maybe", "properly": "", "when": "if",
31 "places": "positions", "position": "positions", "move": "shift", "shifted": "shift",
32 "characters": "character", "chars": "character", "bytes": "byte", "letters": "letter"}
33STOP = {"a", "an", "the", "one", "of", "and", "or", "is", "are", "it", "its", "be", "by", "to", "in", "on",
34 "for", "with", "that", "this", "they", "not", "if", "else", "made", "only", "each", "every", "all",
35 "as", "at", "from", "so", "then", "n", "maybe", "when", "there", "which", ""}
36
37
38def norm(s):
39 s = re.sub(r"\d+", " N ", s.lower())
40 s = re.sub(r"[^a-z ]", " ", s)
41 return [SYN.get(w, w) for w in s.split()]
42
43
44def content(ws):
45 return {w for w in ws if w not in STOP}
46
47
48def grams(ws, k=5):
49 t = " ".join(ws)
50 return {t[i:i + k] for i in range(max(1, len(t) - k + 1))}
51
52
53def jac(a, b):
54 return len(a & b) / len(a | b) if (a or b) else 1.0
55
56
57def ovl(a, b):
58 return len(a & b) / min(len(a), len(b)) if (a and b) else 0.0
59
60
61def load(root):
62 out = []
63 for split in sorted(os.listdir(root)):
64 d = os.path.join(root, split)
65 if not os.path.isdir(d):
66 continue
67 for tid in sorted(os.listdir(d)):
68 p = os.path.join(d, tid, "statement.txt")
69 if os.path.isfile(p):
70 out.append((split, tid, open(p).read().strip()))
71 return out
72
73
74
75PLANT1 = ("Read a line made only of ( and ) characters, maybe empty. Output YES when the brackets are "
76 "properly balanced and NO when they are not.")
77PLANT2 = ("Move each alphabet character 7 positions forward, so y becomes f, keep upper and lower case, "
78 "and pass every other byte through untouched.")
79
80
81def rule(sw, so, sg):
82 return sw >= THRESH or sg >= THRESH or (so >= 0.9 and sw >= 0.45)
83
84
85def main():
86 pub, sealed = sys.argv[1], sys.argv[2]
87 rpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "leak-reviews.json")
88 reviews = json.load(open(rpath)) if os.path.exists(rpath) else {}
89 rows = load(pub) + load(sealed)
90 n_real = len(rows)
91 rows += [("heldA", "zz-plant1.v1", PLANT1), ("heldA", "zz-plant2.v1", PLANT2)]
92 feats = []
93 for s, t, x in rows:
94 ws = norm(x)
95 feats.append((s, t, set(ws), content(ws), grams(ws)))
96 flags, best, plant = [], 0.0, {"zz-plant1": 0.0, "zz-plant2": 0.0}
97 for (s1, t1, w1, c1, g1), (s2, t2, w2, c2, g2) in itertools.combinations(feats, 2):
98 f1, f2 = t1.split(".")[0], t2.split(".")[0]
99 if f1 == f2:
100 continue
101 sw, so, sg = jac(w1, w2), ovl(c1, c2), jac(g1, g2)
102 m = max(sw, so, sg)
103 pz = [f for f in (f1, f2) if f.startswith("zz-")]
104 if pz:
105 if len(pz) == 1 and rule(sw, so, sg):
106 plant[pz[0]] = 1.0
107 continue
108 best = max(best, m)
109 if rule(sw, so, sg):
110 flags.append({"a": opaque(s1, t1), "b": opaque(s2, t2), "word": round(sw, 3),
111 "content_overlap": round(so, 3), "char5": round(sg, 3), "cross_split": s1 != s2})
112 key = flags[-1]["a"] + " | " + flags[-1]["b"]
113 flags[-1]["review"] = reviews.get(key)
114 res = {"version": 2, "tasks": n_real, "pairs_checked": n_real * (n_real - 1) // 2, "threshold": THRESH,
115 "max_cross_family_similarity": round(best, 3), "n_flags": len(flags),
116 "n_flags_cross_split": sum(f["cross_split"] for f in flags), "flags": flags,
117 "plant1_flagged": plant["zz-plant1"] > 0, "plant2_flagged": plant["zz-plant2"] > 0}
118 print(json.dumps(res, indent=1))
119 if not res["plant1_flagged"]:
120 print("LEAK CONTROL FAIL planted duplicate not flagged")
121 sys.exit(1)
122 unreviewed = [f for f in flags if not f["review"]]
123 dup = [f for f in flags if f["cross_split"] and (f["review"] or "").startswith("duplicate")]
124 for f in unreviewed:
125 print("LEAK UNREVIEWED", f["a"], "|", f["b"])
126 for f in dup:
127 print("LEAK DUPLICATE ACROSS SPLITS", f["a"], "|", f["b"])
128 sys.exit(1 if unreviewed or dup else 0)
129
130
131if __name__ == "__main__":
132 main()