Download validator.py from RegalFire/GameAgent-Horizon-120K: direct link, hf CLI and curl.
- Browser
- Download file 6.43 kB
-
https://huggingface.co/datasets/RegalFire/GameAgent-Horizon-120K/resolve/main/validator.py
- Command line
-
hf download hf://datasets/RegalFire/GameAgent-Horizon-120K/validator.py
-
curl -L -o validator.py https://huggingface.co/datasets/RegalFire/GameAgent-Horizon-120K/resolve/main/validator.py
6.43 kB
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| from collections import Counter | |
| from pathlib import Path | |
| FAMILIES = { | |
| "key_then_door", | |
| "resource_then_craft", | |
| "switch_then_gate", | |
| "heal_then_combat", | |
| "collect_then_extract", | |
| "tool_then_repair", | |
| "navigate_then_interact", | |
| "prerequisite_chain", | |
| } | |
| 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(row): | |
| fam = row["family"] | |
| s = row["observation"]["state"] | |
| if fam == "key_then_door": | |
| have_key = bool(s["inventory"]) | |
| action = "interact" if have_key else "pickup" | |
| ns = dict(s) | |
| if action == "pickup": | |
| key = f"key_{row_index_hint(row)%17}" | |
| # recover visible key by expected inventory if needed | |
| if row["expected_state_t1"]["inventory"]: | |
| key = row["expected_state_t1"]["inventory"][0] | |
| ns["inventory"] = [key] | |
| ns["key_visible"] = False | |
| else: | |
| ns["door_locked"] = False | |
| return action, ns | |
| if fam == "resource_then_craft": | |
| wood, metal = s["wood"], s["metal"] | |
| ns = dict(s) | |
| if wood < 2: | |
| action = "pickup" | |
| ns["wood"] += 1 | |
| elif metal < 1: | |
| action = "pickup" | |
| ns["metal"] += 1 | |
| else: | |
| action = "craft" | |
| ns["wood"] -= 2 | |
| ns["metal"] -= 1 | |
| ns["tool_crafted"] = True | |
| return action, ns | |
| if fam == "switch_then_gate": | |
| ns = dict(s) | |
| if not s["switch_on"]: | |
| action = "interact" | |
| ns["switch_on"] = True | |
| ns["gate_open"] = True | |
| else: | |
| action = "move_north" | |
| ns["at_switch"] = False | |
| return action, ns | |
| if fam == "heal_then_combat": | |
| ns = dict(s) | |
| if s["hp"] < 40 and s["medkits"] > 0: | |
| action = "heal" | |
| ns["hp"] = min(100, s["hp"] + 35) | |
| ns["medkits"] -= 1 | |
| else: | |
| action = "attack" | |
| ns["enemy_hp"] = max(0, s["enemy_hp"] - 25) | |
| return action, ns | |
| if fam == "collect_then_extract": | |
| ns = dict(s) | |
| if s["items_collected"] < s["items_required"]: | |
| action = "pickup" | |
| ns["items_collected"] += 1 | |
| else: | |
| action = "extract" | |
| ns["at_extraction"] = True | |
| return action, ns | |
| if fam == "tool_then_repair": | |
| ns = dict(s) | |
| if not s["has_tool"]: | |
| action = "pickup" | |
| ns["has_tool"] = True | |
| else: | |
| action = "repair" | |
| ns["machine_damaged"] = False | |
| ns["machine_operational"] = True | |
| return action, ns | |
| if fam == "navigate_then_interact": | |
| ns = dict(s) | |
| if s["distance_to_target"] > 0: | |
| action = "move_east" | |
| ns["distance_to_target"] -= 1 | |
| else: | |
| action = "interact" | |
| ns["target_interacted"] = True | |
| return action, ns | |
| if fam == "prerequisite_chain": | |
| stage = s["stage"] | |
| ns = dict(s) | |
| if stage == 0: | |
| action = "pickup" | |
| ns["stage"] = 1 | |
| ns["token"] = True | |
| elif stage == 1: | |
| action = "interact" | |
| ns["stage"] = 2 | |
| ns["terminal_unlocked"] = True | |
| elif stage == 2: | |
| action = "interact" | |
| ns["stage"] = 3 | |
| ns["vault_open"] = True | |
| else: | |
| action = "extract" | |
| ns["stage"] = 4 | |
| return action, ns | |
| raise ValueError(fam) | |
| def row_index_hint(row): | |
| return row["seed"] | |
| def validate(path): | |
| ids=set() | |
| fps=set() | |
| fams=Counter() | |
| splits=Counter() | |
| difficulties=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["family"] not in FAMILIES: | |
| raise ValueError("family") | |
| if r["id"] in ids: | |
| raise ValueError("duplicate id") | |
| ids.add(r["id"]) | |
| if r["fingerprint_sha256"] in fps: | |
| raise ValueError("duplicate fingerprint") | |
| 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']}") | |
| with_id=dict(body) | |
| with_id["id"]=r["id"] | |
| if sha(with_id) != r["fingerprint_sha256"]: | |
| raise ValueError(f"fingerprint mismatch {r['id']}") | |
| action, next_state = oracle(r) | |
| if action != r["optimal_action"]: | |
| raise ValueError(f"action mismatch {r['id']}") | |
| if action not in r["available_actions"]: | |
| raise ValueError(f"optimal missing {r['id']}") | |
| if next_state != r["expected_state_t1"]: | |
| raise ValueError(f"next state mismatch {r['id']}") | |
| if len(set(r["available_actions"])) != len(r["available_actions"]): | |
| raise ValueError(f"duplicate actions {r['id']}") | |
| p=r["provenance"] | |
| if ( | |
| not p["synthetic"] | |
| or p["copied_records"] != 0 | |
| or p["contains_pii"] | |
| or p["copyrighted_gameplay"] | |
| or p["llm_generated_ground_truth"] | |
| ): | |
| raise ValueError("provenance") | |
| fams[r["family"]] += 1 | |
| splits[r["split"]] += 1 | |
| difficulties[r["difficulty"]] += 1 | |
| return { | |
| "status":"PASS", | |
| "validated":count, | |
| "unique_ids":len(ids), | |
| "unique_fingerprints":len(fps), | |
| "families":dict(fams), | |
| "splits":dict(splits), | |
| "difficulties":dict(difficulties), | |
| } | |
| 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() | |