Download sample/scorer/run_naive.py from zero-trust-physics/zero-trust-physics-sample-group-2: direct link, hf CLI and curl.
- Browser
- Download file 9.03 kB
-
https://huggingface.co/datasets/zero-trust-physics/zero-trust-physics-sample-group-2/resolve/main/sample/scorer/run_naive.py
- Command line
-
hf download hf://datasets/zero-trust-physics/zero-trust-physics-sample-group-2/sample/scorer/run_naive.py
-
curl -L -o run_naive.py https://huggingface.co/datasets/zero-trust-physics/zero-trust-physics-sample-group-2/resolve/main/sample/scorer/run_naive.py
9.03 kB
| """A one-call agent: one forward request at a plausible default, and its answer reported. | |
| python3 run_naive.py --engine ENGINE --environments F --out DIR [--only id,id] | |
| It shows what a single tool call earns on a set. For a tradeoff or chain_design environment it sends | |
| one run at the middle of the design's range (no solve: one call) and gives it as both designs; a staged design | |
| sends its first stage's request that way, and a solve design a solve. For a | |
| budget environment it sends the task's request at the middle of the stated span and gives it as both | |
| sides, naming the task's stop. For a forensics table it sends the first row's request and calls every | |
| row genuine. Calls go through harness/mcp_log_proxy.py, as in run_reference.py. | |
| python3 score.py DIR --engine ENGINE --environments F --calls-from harness | |
| python3 run_naive.py --gate DIR --environments F --scores F | |
| --gate reads what the run left. A task with no reply, no logged call, or no score row, or a score of "the reply is empty", | |
| stops it with exit 1: a 0 with nothing behind it is not a one-call score. It prints the counts, solve-shape rows and calls | |
| the verifier answered with an error among them. | |
| Licence: Apache-2.0. | |
| """ | |
| import argparse | |
| import copy | |
| import json | |
| import math | |
| import os | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| from run_reference import proxied # noqa: E402 | |
| from score import close_verifier, load_environments, open_verifier, set_path # noqa: E402 | |
| DEFAULTS = {"aerospace/capsule_stardust": 8.21, "energy/reactor_triga_jsi": 2.5, "energy/reactor_spert3": 1.0, | |
| "energy/cell_2170_model3": 5.0, "energy/grid_gb": 1000.0} | |
| def middle(env, spec): | |
| if spec.get("range"): | |
| return 0.5 * sum(spec["range"]) | |
| if env["models"][0] in DEFAULTS: | |
| return DEFAULTS[env["models"][0]] | |
| return float(env["reference"]["boundary"]) * 0.9 # a guess near the answer, one call only | |
| def naive_reply(env, verifier): | |
| calls = [] | |
| family = env["family"] | |
| if family in ("tradeoff", "chain_design"): | |
| spec = env["check"]["design"] | |
| var = spec["variable"] | |
| if spec.get("stages") or spec.get("tool") == "solve" or spec.get("kind") == "solve": | |
| # a staged design has no check.design.template (its first stage is sent), and a solve design is sent as a solve, not a run | |
| key, tool, template = (spec["stages"][0]["key"], spec["stages"][0]["tool"], spec["stages"][0]["template"]) if spec.get("stages") else ("solve", "solve", spec["template"]) | |
| q = copy.deepcopy(template) | |
| if not spec.get("stages") or var.get("stage") == key: | |
| for place in [var["path"]] + list(var.get("also") or []): | |
| set_path(q, place, middle(env, spec)) | |
| calls.append({"tool": tool, "arguments": q}) | |
| body, is_error = verifier.call_json(tool, q) | |
| proof = (body.get("run_seal") or body.get("solution_seal")) if isinstance(body, dict) else None | |
| one = {key: {"request": {"tool": tool, "arguments": q}, "proof": proof}} | |
| card = {"passing": one, "failing": one, "binding": env["check"]["constraints"][0]["id"]} | |
| return "One request at the middle of the range.\n\n" + json.dumps(card), calls | |
| run = copy.deepcopy(spec["template"]) | |
| if var["kind"] == "entry_angle_deg": | |
| g = math.radians(middle(env, spec)) | |
| run["initial"]["radial_speed_m_s"] = -var["speed_m_s"] * math.sin(g) | |
| run["initial"]["transverse_speed_m_s"] = var["speed_m_s"] * math.cos(g) | |
| elif var.get("on", "run") == "run" and var["path"].startswith("initial.start_temperature_k"): | |
| set_path(run, var["path"], 296.15) | |
| elif var.get("on", "run") == "run": | |
| value = middle(env, spec) | |
| for place in [var["path"]] + list(var.get("also") or []): | |
| set_path(run, place, value) | |
| calls.append({"tool": "run_trajectory", "arguments": run}) | |
| body, is_error = verifier.call_json("run_trajectory", run) | |
| proof = body.get("run_seal") if isinstance(body, dict) else None | |
| one = {"run": {"request": {"tool": "run_trajectory", "arguments": run}, "proof": proof}} | |
| card = {"passing": one, "failing": one, "binding": env["check"]["constraints"][0]["id"]} | |
| words = "One run at the default design." | |
| elif family == "budget": | |
| lo, hi = env["reference"].get("span") or (env["reference"]["boundary"] * 0.9, env["reference"]["boundary"] * 1.1) | |
| request = copy.deepcopy(env["check"]["template"]) | |
| for place in [env["check"]["variable"]] + list(env["check"].get("also") or []): | |
| set_path(request, place, 0.5 * (lo + hi)) | |
| calls.append({"tool": env["check"]["tool"], "arguments": request}) | |
| body, is_error = verifier.call_json(env["check"]["tool"], request) | |
| proof = (body.get("run_seal") or body.get("solution_seal")) if isinstance(body, dict) else None | |
| side = {"request": {"tool": env["check"]["tool"], "arguments": request}, "proof": proof} | |
| card = {"holds": side, "stops": dict(side, stopped=env["check"]["stop"])} | |
| words = "One run at the middle of the span." | |
| else: | |
| first = env["check"]["rows"][0]["request"] | |
| calls.append(first) | |
| verifier.call_json(first["tool"], first["arguments"]) | |
| card = {"false_rows": {}} | |
| words = "The first row came back; every row is taken as genuine." | |
| return words + "\n\n" + json.dumps(card), calls | |
| def gate(folder, environments_path, scores_path): | |
| environments = load_environments(environments_path) | |
| scores = {} | |
| if os.path.isfile(scores_path): | |
| with open(scores_path, encoding="utf-8") as f: | |
| scores = {r["id"]: r for r in (json.loads(line) for line in f if line.strip())} | |
| missing, errors, solve_shape, staged = [], 0, 0, 0 | |
| for env in environments: | |
| tid = env["id"] | |
| spec = env["check"].get("design") or {} | |
| staged += bool(spec.get("stages")) | |
| solve_shape += spec.get("tool") == "solve" or spec.get("kind") == "solve" or env["check"].get("tool") == "solve" | |
| reply = os.path.join(folder, tid + ".txt") | |
| log = os.path.join(folder, tid + ".harness.jsonl") | |
| lines = [] | |
| if os.path.isfile(log): | |
| with open(log, encoding="utf-8") as f: | |
| lines = [json.loads(line) for line in f if line.strip()] | |
| errors += sum(1 for line in lines if line.get("is_error")) | |
| row = scores.get(tid) | |
| if not os.path.isfile(reply) or not open(reply, encoding="utf-8").read().strip(): | |
| missing.append(tid + " (no reply)") | |
| elif not lines: | |
| missing.append(tid + " (no call)") | |
| elif row is None: | |
| missing.append(tid + " (no score)") | |
| elif row.get("reason") == "the reply is empty": | |
| missing.append(tid + " (empty reply)") | |
| rows = [scores[e["id"]] for e in environments if e["id"] in scores] | |
| print("one-call agent: replied on %d of %d; reward above 0 on %d, graded above 0 on %d; solve-shape rows %d, staged rows %d; calls answered with an error %d" % ( | |
| len(environments) - len(missing), len(environments), sum(1 for r in rows if r.get("reward")), sum(1 for r in rows if r.get("graded")), solve_shape, staged, errors)) | |
| if missing: | |
| sys.exit("NOT a one-call score: " + ", ".join(missing[:8])) | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--engine") | |
| parser.add_argument("--environments", required=True) | |
| parser.add_argument("--out") | |
| parser.add_argument("--only") | |
| parser.add_argument("--direct", action="store_true") | |
| parser.add_argument("--gate", metavar="DIR") | |
| parser.add_argument("--scores") | |
| args = parser.parse_args() | |
| if args.gate: | |
| return gate(args.gate, args.environments, args.scores or "") | |
| if not args.engine or not args.out: | |
| parser.error("give --engine and --out") | |
| environments = load_environments(args.environments) | |
| if args.only: | |
| wanted = set(args.only.split(",")) | |
| environments = [e for e in environments if e["id"] in wanted] | |
| os.makedirs(args.out, exist_ok=True) | |
| shared = open_verifier(args.engine) if args.direct else None | |
| try: | |
| for env in environments: | |
| verifier = shared or proxied(args.engine, os.path.join(args.out, env["id"] + ".harness.jsonl")) | |
| try: | |
| reply, calls = naive_reply(env, verifier) | |
| finally: | |
| if not shared: | |
| close_verifier(verifier) | |
| with open(os.path.join(args.out, env["id"] + ".txt"), "w", encoding="utf-8") as f: | |
| f.write(reply + "\n") | |
| with open(os.path.join(args.out, env["id"] + ".calls.jsonl"), "w", encoding="utf-8") as f: | |
| for c in calls: | |
| f.write(json.dumps(c) + "\n") | |
| finally: | |
| if shared: | |
| close_verifier(shared) | |
| if __name__ == "__main__": | |
| main() | |