Add deterministic wrong-answer sanity check
Browse files#!/usr/bin/env python3
"""Random-wrong sanity check: a deliberately wrong deterministic reply should score 0."""
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "environment" / "src"))
from binary_reasoning_v1.grading import _answer_symbol, grade_item, needs_judge, norm_symbol # noqa: E402
def resolve_path():
if len(sys.argv) > 1:
return Path(sys.argv[1])
for p in (ROOT / "sample_free.jsonl", ROOT / "environment/src/binary_reasoning_v1/items.jsonl"):
if p.exists():
return p
raise SystemExit("no sample JSONL found; pass one as argv[1]")
def load_rows(path):
rows = []
for line in path.read_text(encoding="utf-8").splitlines():
if line.strip():
rows.append(json.loads(line))
return rows
def parse_json_answer(ans):
if isinstance(ans, dict):
return ans
if isinstance(ans, str):
try:
obj = json.loads(ans)
if isinstance(obj, dict):
return obj
except Exception:
pass
return {}
def mutate_token(s):
s = str(s)
if not s:
return "RELAB_WRONG"
mid = len(s) // 2
return f"{s[:mid]}_RELAB_WRONG_{s[mid:]}"
def wrong_reply(row):
fam = row.get("family", "")
ans = row.get("answer")
if fam == "func_id_mc" and isinstance(ans, dict):
opts = ans.get("options", [])
correct = norm_symbol(ans.get("correct", ""))
for i, opt in enumerate(opts[:4]):
if norm_symbol(opt) != correct:
return "ABCD"[i]
return "RELAB_WRONG"
if fam == "toolchain_fp" and isinstance(ans, dict):
parts = [mutate_token(s) for s in ans.get("toolchain_strings", []) if str(s)]
return "\n".join(parts) if parts else "RELAB_WRONG"
if fam == "library_ident":
ans_str = str(_answer_symbol(ans) or ans or "")
toks = [t for t in ans_str.split() if len(t) >= 2]
return " ".join(mutate_token(t) for t in toks) if toks else "RELAB_WRONG"
if fam == "crossarch_equivalence":
obj = parse_json_answer(ans)
if bool(obj.get("same")):
return "Not the same."
return "Same function: RELAB_WRONG"
sym = _answer_symbol(ans) or ans or ""
if row.get("family") == "agentic_flag_hunt":
return mutate_token(sym)
return f"{sym}_RELAB_WRONG"
def main():
path = resolve_path()
rows = load_rows(path)
passed = failed = skipped = 0
fails = []
for row in rows:
fam = row.get("family", "")
ans = row.get("answer")
if needs_judge(fam, ans):
skipped += 1
continue
score = grade_item(fam, wrong_reply(row), ans)
if score == 0.0:
passed += 1
else:
failed += 1
fails.append((row.get("id"), fam, score))
print(f"file={path}")
print(f"summary: random_wrong pass={passed}/{passed + failed} fail={failed} skipped_judge={skipped}")
for f in fails[:10]:
print("FAIL", *f)
return 0 if failed == 0 else 1
if __name__ == "__main__":
raise SystemExit(main())
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Random-wrong sanity check: a deliberately wrong deterministic reply should score 0."""
|
| 3 |
+
import json
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 8 |
+
sys.path.insert(0, str(ROOT / "environment" / "src"))
|
| 9 |
+
from binary_reasoning_v1.grading import _answer_symbol, grade_item, needs_judge, norm_symbol # noqa: E402
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def resolve_path():
|
| 13 |
+
if len(sys.argv) > 1:
|
| 14 |
+
return Path(sys.argv[1])
|
| 15 |
+
for p in (ROOT / "sample_free.jsonl", ROOT / "environment/src/binary_reasoning_v1/items.jsonl"):
|
| 16 |
+
if p.exists():
|
| 17 |
+
return p
|
| 18 |
+
raise SystemExit("no sample JSONL found; pass one as argv[1]")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_rows(path):
|
| 22 |
+
rows = []
|
| 23 |
+
for line in path.read_text(encoding="utf-8").splitlines():
|
| 24 |
+
if line.strip():
|
| 25 |
+
rows.append(json.loads(line))
|
| 26 |
+
return rows
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def parse_json_answer(ans):
|
| 30 |
+
if isinstance(ans, dict):
|
| 31 |
+
return ans
|
| 32 |
+
if isinstance(ans, str):
|
| 33 |
+
try:
|
| 34 |
+
obj = json.loads(ans)
|
| 35 |
+
if isinstance(obj, dict):
|
| 36 |
+
return obj
|
| 37 |
+
except Exception:
|
| 38 |
+
pass
|
| 39 |
+
return {}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def mutate_token(s):
|
| 43 |
+
s = str(s)
|
| 44 |
+
if not s:
|
| 45 |
+
return "RELAB_WRONG"
|
| 46 |
+
mid = len(s) // 2
|
| 47 |
+
return f"{s[:mid]}_RELAB_WRONG_{s[mid:]}"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def wrong_reply(row):
|
| 51 |
+
fam = row.get("family", "")
|
| 52 |
+
ans = row.get("answer")
|
| 53 |
+
if fam == "func_id_mc" and isinstance(ans, dict):
|
| 54 |
+
opts = ans.get("options", [])
|
| 55 |
+
correct = norm_symbol(ans.get("correct", ""))
|
| 56 |
+
for i, opt in enumerate(opts[:4]):
|
| 57 |
+
if norm_symbol(opt) != correct:
|
| 58 |
+
return "ABCD"[i]
|
| 59 |
+
return "RELAB_WRONG"
|
| 60 |
+
if fam == "toolchain_fp" and isinstance(ans, dict):
|
| 61 |
+
parts = [mutate_token(s) for s in ans.get("toolchain_strings", []) if str(s)]
|
| 62 |
+
return "\n".join(parts) if parts else "RELAB_WRONG"
|
| 63 |
+
if fam == "library_ident":
|
| 64 |
+
ans_str = str(_answer_symbol(ans) or ans or "")
|
| 65 |
+
toks = [t for t in ans_str.split() if len(t) >= 2]
|
| 66 |
+
return " ".join(mutate_token(t) for t in toks) if toks else "RELAB_WRONG"
|
| 67 |
+
if fam == "crossarch_equivalence":
|
| 68 |
+
obj = parse_json_answer(ans)
|
| 69 |
+
if bool(obj.get("same")):
|
| 70 |
+
return "Not the same."
|
| 71 |
+
return "Same function: RELAB_WRONG"
|
| 72 |
+
sym = _answer_symbol(ans) or ans or ""
|
| 73 |
+
if row.get("family") == "agentic_flag_hunt":
|
| 74 |
+
return mutate_token(sym)
|
| 75 |
+
return f"{sym}_RELAB_WRONG"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def main():
|
| 79 |
+
path = resolve_path()
|
| 80 |
+
rows = load_rows(path)
|
| 81 |
+
passed = failed = skipped = 0
|
| 82 |
+
fails = []
|
| 83 |
+
for row in rows:
|
| 84 |
+
fam = row.get("family", "")
|
| 85 |
+
ans = row.get("answer")
|
| 86 |
+
if needs_judge(fam, ans):
|
| 87 |
+
skipped += 1
|
| 88 |
+
continue
|
| 89 |
+
score = grade_item(fam, wrong_reply(row), ans)
|
| 90 |
+
if score == 0.0:
|
| 91 |
+
passed += 1
|
| 92 |
+
else:
|
| 93 |
+
failed += 1
|
| 94 |
+
fails.append((row.get("id"), fam, score))
|
| 95 |
+
print(f"file={path}")
|
| 96 |
+
print(f"summary: random_wrong pass={passed}/{passed + failed} fail={failed} skipped_judge={skipped}")
|
| 97 |
+
for f in fails[:10]:
|
| 98 |
+
print("FAIL", *f)
|
| 99 |
+
return 0 if failed == 0 else 1
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
raise SystemExit(main())
|
| 104 |
+
Add deterministic wrong-answer sanity check
|