diff options
| author | fitzypopper <fitzypopper@proton.me> | 2026-11-24 15:09:20 +0100 |
|---|---|---|
| committer | fitzypopper <fitzypopper@proton.me> | 2026-11-24 15:09:20 +0100 |
| commit | 5ba7f3548f4dbeb429feb912f2fa87a1ed33fe3f (patch) | |
| tree | dcecc03367ce1288a8db2a88e75f3247ec1866c2 /scripts | |
| parent | d0da7dade8b688f2e0f7f23f4155d6cbb438809e (diff) | |
Add counts-only corpus regression watch tool and tests (CORPUS-WATCH)
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/corpus_watch.py | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/scripts/corpus_watch.py b/scripts/corpus_watch.py new file mode 100644 index 000000000..8d12ea69f --- /dev/null +++ b/scripts/corpus_watch.py | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | """Counts-only corpus regression watch. | ||
| 3 | |||
| 4 | Runs `make_game.py --stages check,shaders` against your own copy, then records | ||
| 5 | COUNTS ONLY (carved / translated / spirv-val-validated, per stage) against the | ||
| 6 | checked-out commit. This is the same counting the progress dashboard uses, so | ||
| 7 | a drop between snapshots is a regression. | ||
| 8 | |||
| 9 | Nothing derived from game data is written to the results file: no shader names, | ||
| 10 | no carved file names, no paths into the game copy. Diagnostic logs stay local | ||
| 11 | and may contain paths; never publish them. | ||
| 12 | |||
| 13 | python3 -I scripts/corpus_watch.py --game <pkg> --work <work> --out <out> \ | ||
| 14 | --report <folder outside the repo> --results <counts.jsonl> --once | ||
| 15 | python3 -I scripts/corpus_watch.py ... --interval 6 --pull | ||
| 16 | |||
| 17 | Standard library only; all text writes name their line ending. | ||
| 18 | """ | ||
| 19 | import argparse | ||
| 20 | import json | ||
| 21 | import os | ||
| 22 | import shutil | ||
| 23 | import subprocess | ||
| 24 | import sys | ||
| 25 | import time | ||
| 26 | from datetime import datetime, timezone | ||
| 27 | |||
| 28 | # -I removes the script folder from sys.path, so add tools/ explicitly. | ||
| 29 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tools")) | ||
| 30 | from report_paths import same_report_path # noqa: E402 | ||
| 31 | |||
| 32 | MIN_FREE = 40 * 1024**3 | ||
| 33 | |||
| 34 | |||
| 35 | def corpus_counts(work): | ||
| 36 | """Return counts-only dict, or None when the shader stage produced no indexes. | ||
| 37 | |||
| 38 | Mirrors tools/progress_dashboard.py: entries in dxbc/index.json are carved; | ||
| 39 | entries in spirv/index.json without an 'error' key passed spirv-val. | ||
| 40 | """ | ||
| 41 | dxbc = os.path.join(work, "shaders", "dxbc", "index.json") | ||
| 42 | spirv_path = os.path.join(work, "shaders", "spirv", "index.json") | ||
| 43 | if not os.path.isfile(dxbc): | ||
| 44 | old_dxbc = os.path.join(work, "dxbc", "index.json") | ||
| 45 | if not os.path.isfile(old_dxbc): | ||
| 46 | return None | ||
| 47 | dxbc = old_dxbc | ||
| 48 | spirv_path = os.path.join(work, "spirv", "index.json") | ||
| 49 | |||
| 50 | with open(dxbc, encoding="utf-8") as f: | ||
| 51 | carved = json.load(f) | ||
| 52 | entries = carved if isinstance(carved, list) else list(carved.values()) | ||
| 53 | |||
| 54 | per_stage = {} | ||
| 55 | validated = 0 | ||
| 56 | spirv_entries = 0 | ||
| 57 | if os.path.isfile(spirv_path): | ||
| 58 | with open(spirv_path, encoding="utf-8") as f: | ||
| 59 | spirv = json.load(f) | ||
| 60 | spirv_entries = len(spirv) | ||
| 61 | for meta in spirv.values(): | ||
| 62 | if isinstance(meta, dict) and "error" not in meta: | ||
| 63 | stage = meta.get("stage") or "unknown" | ||
| 64 | per_stage.setdefault(stage, [0, 0])[1] += 1 | ||
| 65 | validated += 1 | ||
| 66 | for entry in entries: | ||
| 67 | stage = (entry.get("stage") if isinstance(entry, dict) else None) or "unknown" | ||
| 68 | per_stage.setdefault(stage, [0, 0])[0] += 1 | ||
| 69 | |||
| 70 | return {"carved": len(entries), "spirv_entries": spirv_entries, | ||
| 71 | "validated": validated, "per_stage": per_stage} | ||
| 72 | |||
| 73 | |||
| 74 | def outputs_collide(work, report, results, log): | ||
| 75 | """Refuse an output that names an input or another output.""" | ||
| 76 | outputs = [p for p in (report, results, log) if p] | ||
| 77 | for i, first in enumerate(outputs): | ||
| 78 | for second in outputs[i + 1:]: | ||
| 79 | if same_report_path(first, second): | ||
| 80 | return True | ||
| 81 | for out in outputs: | ||
| 82 | if same_report_path(out, work): | ||
| 83 | return True | ||
| 84 | return False | ||
| 85 | |||
| 86 | |||
| 87 | def main(): | ||
| 88 | ap = argparse.ArgumentParser(description=__doc__) | ||
| 89 | ap.add_argument("--game", required=True, help="your package folder") | ||
| 90 | ap.add_argument("--work", required=True, help="work folder (outside the repo)") | ||
| 91 | ap.add_argument("--out", required=True, help="built port folder (outside the repo)") | ||
| 92 | ap.add_argument("--report", required=True, help="progress dashboard folder (outside the repo)") | ||
| 93 | ap.add_argument("--results", required=True, help="counts-only JSONL file") | ||
| 94 | ap.add_argument("--log", help="local diagnostic log (may contain paths; never publish)") | ||
| 95 | ap.add_argument("--stages", default="check,shaders") | ||
| 96 | ap.add_argument("--interval", type=float, metavar="HOURS", default=0) | ||
| 97 | ap.add_argument("--once", action="store_true") | ||
| 98 | ap.add_argument("--pull", action="store_true", help="git pull --ff-only before each run") | ||
| 99 | ap.add_argument("--github", action="store_true", help="also refresh dashboard PR/CI panels") | ||
| 100 | ap.add_argument("--jobs", type=int) | ||
| 101 | a = ap.parse_args() | ||
| 102 | if not a.once and a.interval <= 0: | ||
| 103 | ap.error("use --once or --interval HOURS") | ||
| 104 | |||
| 105 | repo = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 106 | log_path = a.log | ||
| 107 | |||
| 108 | def log(msg): | ||
| 109 | line = f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}" | ||
| 110 | print(line, flush=True) | ||
| 111 | if log_path: | ||
| 112 | with open(log_path, "a", encoding="utf-8", newline="\n") as f: | ||
| 113 | f.write(line + "\n") | ||
| 114 | |||
| 115 | if outputs_collide(a.work, a.report, a.results, log_path): | ||
| 116 | ap.error("an output names the work folder or another output") | ||
| 117 | |||
| 118 | for tool in ("llvm-mc", "spirv-val", "git"): | ||
| 119 | if not shutil.which(tool): | ||
| 120 | ap.error(f"missing tool: {tool}") | ||
| 121 | if not os.path.isdir(a.game): | ||
| 122 | ap.error("game folder not found") | ||
| 123 | probe = subprocess.run( | ||
| 124 | ["llvm-mc", "-triple=amdgcn-amd-amdhsa", "-mcpu=gfx1013", | ||
| 125 | "-filetype=obj", "-o", os.devnull], | ||
| 126 | input="s_nop 0\n", text=True, capture_output=True) | ||
| 127 | if probe.returncode != 0: | ||
| 128 | ap.error("llvm-mc cannot assemble gfx1013 (AMDGPU target missing)") | ||
| 129 | |||
| 130 | def run(cmd, timeout): | ||
| 131 | return subprocess.run(cmd, cwd=repo, text=True, stdout=subprocess.PIPE, | ||
| 132 | stderr=subprocess.STDOUT, timeout=timeout) | ||
| 133 | |||
| 134 | def run_once(): | ||
| 135 | if a.pull: | ||
| 136 | r = run(["git", "pull", "--ff-only"], 300) | ||
| 137 | if r.returncode != 0: | ||
| 138 | log("git pull failed:\n" + r.stdout[-2000:]) | ||
| 139 | return 1 | ||
| 140 | sha = run(["git", "rev-parse", "HEAD"], 60).stdout.strip() | ||
| 141 | log(f"commit {sha}") | ||
| 142 | for folder in (a.work, a.out, a.report): | ||
| 143 | os.makedirs(folder, exist_ok=True) | ||
| 144 | cmd = [sys.executable, "-I", "make_game.py", "--game", a.game, | ||
| 145 | "--work", a.work, "--out", a.out, "--stages", a.stages] | ||
| 146 | if a.jobs: | ||
| 147 | cmd += ["--jobs", str(a.jobs)] | ||
| 148 | log("running make_game.py (paths omitted from the local log)") | ||
| 149 | r = run(cmd, 6 * 3600) | ||
| 150 | if log_path: | ||
| 151 | with open(log_path, "a", encoding="utf-8", newline="\n") as f: | ||
| 152 | f.write(r.stdout + "\n") | ||
| 153 | log(f"make_game.py exit {r.returncode}") | ||
| 154 | |||
| 155 | counts = corpus_counts(a.work) | ||
| 156 | record = {"time": datetime.now(timezone.utc).isoformat(), "commit": sha, | ||
| 157 | "make_game_exit": r.returncode, "counts": counts} | ||
| 158 | with open(a.results, "a", encoding="utf-8", newline="\n") as f: | ||
| 159 | f.write(json.dumps(record, sort_keys=True) + "\n") | ||
| 160 | |||
| 161 | dash = [sys.executable, "-I", "tools/progress_dashboard.py", | ||
| 162 | "--work", a.work, "--out", a.report, "--run-tests"] | ||
| 163 | if a.github: | ||
| 164 | dash.append("--github") | ||
| 165 | d = run(dash, 900) | ||
| 166 | log(f"dashboard exit {d.returncode}") | ||
| 167 | |||
| 168 | if counts: | ||
| 169 | log(f"COUNTS carved={counts['carved']} spirv={counts['spirv_entries']} " | ||
| 170 | f"validated={counts['validated']}") | ||
| 171 | for stage, (nc, nv) in sorted(counts["per_stage"].items()): | ||
| 172 | log(f" {stage}: {nv}/{nc} validated") | ||
| 173 | with open(a.results, encoding="utf-8") as f: | ||
| 174 | previous = [json.loads(x) for x in f if x.strip()] | ||
| 175 | if len(previous) >= 2: | ||
| 176 | before = previous[-2].get("counts") or {} | ||
| 177 | log(f"DELTA validated {before.get('validated')} -> {counts['validated']}, " | ||
| 178 | f"carved {before.get('carved')} -> {counts['carved']}") | ||
| 179 | if counts["validated"] < before.get("validated", 0): | ||
| 180 | log("REGRESSION: validated count decreased") | ||
| 181 | else: | ||
| 182 | log("no corpus indexes produced") | ||
| 183 | log(f"counts-only record: {a.results}") | ||
| 184 | return 0 | ||
| 185 | |||
| 186 | rc = run_once() | ||
| 187 | if a.once or a.interval <= 0: | ||
| 188 | return rc | ||
| 189 | while True: | ||
| 190 | time.sleep(a.interval * 3600) | ||
| 191 | run_once() | ||
| 192 | |||
| 193 | |||
| 194 | if __name__ == "__main__": | ||
| 195 | sys.exit(main()) | ||