#!/usr/bin/env python3 r"""Job #1446 -- exercise the shared guard/1 tool on THIS job's real field, at all three scales. This is the integration check of the bounded framework improvement made in this assignment: the guard is a new tool in the shared versioned store (`%LOCALAPPDATA%\solveathome\tools\guard\v1\driftguard.py`, guard/1), and a tool that has only run on its own synthetic fixtures has not been reviewed. Here it reads the actual class-renormalised field of route 31 -- built through the instrument, from the pinned producer, exactly as the three measurement drivers build it -- and its verdict is recorded beside the measurements. The payload is written as .npz; the reports are the artifacts (the payload itself is reproducible from the producer pin in one command and is deliberately not served, being 2 MB of derived floats). Usage: python guardreal.py --producer src/fibre-sign-lag.py --xs 16384,65536,262144 \ --guard --out-dir out """ import argparse import hashlib import importlib.util import json import pathlib import subprocess import sys import numpy as np def load_module(path, name): spec = importlib.util.spec_from_file_location(name, str(path)) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def main(): ap = argparse.ArgumentParser() ap.add_argument("--producer", required=True) ap.add_argument("--xs", default="16384,65536,262144") ap.add_argument("--guard", required=True) ap.add_argument("--instrument", default="src/job1438-cls-resid-offset.py") ap.add_argument("--driver", default="lstar.py") ap.add_argument("--out-dir", default="out") a = ap.parse_args() here = pathlib.Path(__file__).parent ins = load_module(here / a.instrument, "ins") lst = load_module(here / a.driver, "lst") outdir = here / a.out_dir outdir.mkdir(parents=True, exist_ok=True) res = {"tool": "guardreal/1", "job": 1446, "route_id": 31, "guard": {"path": a.guard, "sha256": hashlib.sha256(pathlib.Path(a.guard).read_bytes()).hexdigest()}, "producer": {"path": a.producer, "sha256": hashlib.sha256((here / a.producer).read_bytes()).hexdigest(), "pin_from_instrument": ins.PRODUCER_SHA}, "selftest": None, "scales": {}} st = subprocess.run([sys.executable, a.guard, "selftest"], capture_output=True, text=True, cwd=str(here)) res["selftest"] = json.loads(st.stdout) for x in [int(t) for t in a.xs.split(",")]: s = lst.build(ins, str(here / a.producer), x) c = s["c"] M = len(c) resid = lst.renormalise(s, c) keys = list(s["members"].keys()) groups = np.full(M, -1, dtype=np.int64) d = np.zeros(M) for gi, k in enumerate(keys): idx = np.asarray(s["members"][k], dtype=np.int64) groups[idx] = gi d[idx] = resid[idx] * s["cls_sign"][k] coord = np.arange(M, dtype=float) npz = outdir / ("guard-payload-%d.npz" % x) np.savez_compressed(npz, field=resid, groups=groups, coord=coord) rep = outdir / ("guard-report-%d.json" % x) r = subprocess.run([sys.executable, a.guard, "check", "--npz", str(npz), "--out", str(rep)], capture_output=True, text=True, cwd=str(here)) body = json.loads(rep.read_text(encoding="utf-8")) res["scales"][str(x)] = {"x": x, "J": M, "gates": s["viol"][:3], "guard_exit": r.returncode, "guard_stdout": r.stdout.strip()[-300:], "verdict": body["verdict"], "triggers": body["triggers"], "drift_energy_share": body["drift"]["energy_share_of_field"], "R_before": body["rise"]["R_before"], "R_after_detrend": body["rise"]["R_after_detrend"], "excess_removed_fraction": body["rise"]["excess_removed_fraction"], "spread_before": body["segment_profile"]["spread_before"], "spread_after": body["segment_profile"]["spread_after"], "invariants_ok": body["invariants"]["ok"], "report_sha256": hashlib.sha256(rep.read_bytes()).hexdigest()} print("x=%-8d guard=%s exit=%d %s" % (x, body["verdict"], r.returncode, r.stdout.strip()[-160:])) npz.unlink() # the payload is reproducible; the report is the artifact (outdir / "guardreal.json").write_text(json.dumps(res, indent=1, sort_keys=True), encoding="utf-8", newline="\n") print("guard selftest ok:", res["selftest"]["ok"]) print("wrote", outdir / "guardreal.json") return 0 if __name__ == "__main__": sys.exit(main())