File size: 2,289 Bytes
4882451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""Answer-leak audit: hidden answer labels/tokens should not appear in the model-facing input."""
import json
import re
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


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 candidate_names(row):
    ans = row.get("answer")
    names = []
    if isinstance(ans, dict):
        if isinstance(ans.get("symbol"), str):
            names.append(ans["symbol"])
        if isinstance(ans.get("correct"), str):
            names.append(ans["correct"])
        for alias in ans.get("aliases", []):
            if isinstance(alias, str):
                names.append(alias)
        for s in ans.get("toolchain_strings", []):
            if isinstance(s, str):
                names.append(s)
    elif isinstance(ans, str) and re.fullmatch(r"[A-Za-z_][A-Za-z0-9_.]{4,}", ans):
        # Exact-symbol / flag-style answers. Prose and JSON answers are handled by rubric/judge paths.
        names.append(ans)
    return [n for n in names if len(n) >= 4]


def main():
    path = resolve_path()
    rows = load_rows(path)
    leaks = []
    checked_rows = 0
    ids = [row.get("id") for row in rows]
    for row in rows:
        candidates = candidate_names(row)
        if candidates:
            checked_rows += 1
        surface = row.get("input", "")
        for name in candidates:
            if re.search(r"(?<![A-Za-z0-9_.])" + re.escape(name) + r"(?![A-Za-z0-9_])", surface):
                leaks.append((row.get("id"), row.get("family"), name))
                break
    print(f"file={path}")
    print(f"summary: leak_check items={len(rows)} unique_ids={len(set(ids))} checked_rows={checked_rows} leaks={len(leaks)}")
    for l in leaks[:10]:
        print("LEAK", *l)
    return 0 if not leaks else 1


if __name__ == "__main__":
    raise SystemExit(main())