File size: 3,937 Bytes
73c34cb | 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | from __future__ import annotations
import argparse,json,hashlib
from pathlib import Path
from collections import Counter
def canon(x):
return json.dumps(x,sort_keys=True,separators=(",",":"),ensure_ascii=False)
def sha(x):
return hashlib.sha256(canon(x).encode()).hexdigest()
def oracle(r):
f=r["family"]
s=r["state_before_failure"]
if f=="wrong_target":
ns=dict(s); ns["selected"]="objective_A"
return "select_objective_A",ns
if f=="navigation_loop":
ns=dict(s); ns["x"]=s["x"]+1; ns["visited_same_cell"]=0
return "move_east",ns
if f=="missing_prerequisite":
ns=dict(s); ns["has_key"]=True; ns["key_visible"]=False
return "pickup_key",ns
if f=="premature_interaction":
ns=dict(s); ns["distance_to_terminal"]-=1
return "move_closer",ns
if f=="resource_exhaustion":
ns=dict(s); ns["ammo"]=s["reserve_ammo"]; ns["reserve_ammo"]=0
return "reload",ns
if f=="low_health_attack":
ns=dict(s); ns["hp"]=min(100,s["hp"]+35); ns["medkits"]=0
return "heal",ns
if f=="locked_door":
ns=dict(s); ns["door_locked"]=False
return "unlock_door",ns
if f=="missing_tool":
ns=dict(s); ns["has_tool"]=True; ns["tool_visible"]=False
return "pickup_tool",ns
if f=="failed_extraction":
ns=dict(s); ns["collected"]=s["required"]
return "collect_remaining_item",ns
if f=="wrong_sequence":
ns=dict(s)
if s["stage"]==0:
ns["token"]=True; ns["stage"]=1
return "pickup_token",ns
if s["stage"]==1:
ns["terminal"]=True; ns["stage"]=2
return "unlock_terminal",ns
ns["vault"]=True; ns["stage"]=3
return "open_vault",ns
raise ValueError(f)
def validate(path):
ids=set(); fps=set(); fams=Counter(); splits=Counter(); count=0
with path.open("r",encoding="utf-8") as f:
for line in f:
if not line.strip(): continue
r=json.loads(line); count+=1
if r["id"] in ids: raise ValueError("duplicate id")
if r["fingerprint_sha256"] in fps: raise ValueError("duplicate fingerprint")
ids.add(r["id"]); fps.add(r["fingerprint_sha256"])
body={k:v for k,v in r.items() if k not in {"id","fingerprint_sha256"}}
if sha(body)[:32]!=r["id"]:
raise ValueError(f"id mismatch {r['id']}")
w=dict(body); w["id"]=r["id"]
if sha(w)!=r["fingerprint_sha256"]:
raise ValueError(f"fingerprint mismatch {r['id']}")
action,state=oracle(r)
if action!=r["recovery_action"]:
raise ValueError(f"recovery action {r['id']}")
if state!=r["state_after_recovery"]:
raise ValueError(f"recovery state {r['id']}")
if r["failed_action"]==r["recovery_action"]:
raise ValueError(f"failure equals recovery {r['id']}")
p=r["provenance"]
if (
not p["synthetic"]
or p["copied_records"]!=0
or p["copyrighted_gameplay"]
or p["contains_pii"]
or p["llm_generated_ground_truth"]
):
raise ValueError("provenance")
fams[r["family"]]+=1
splits[r["split"]]+=1
return {
"status":"PASS",
"validated":count,
"unique_ids":len(ids),
"unique_fingerprints":len(fps),
"families":dict(fams),
"splits":dict(splits)
}
def main():
ap=argparse.ArgumentParser()
ap.add_argument("dataset",type=Path)
ap.add_argument("--report",type=Path)
a=ap.parse_args()
report=validate(a.dataset)
if a.report:
a.report.write_text(json.dumps(report,indent=2),encoding="utf-8")
print(json.dumps(report))
if __name__=="__main__":
main()
|