File size: 7,356 Bytes
f3b0a91 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | """alertforge CLI: build | sample | spread.
alertforge build --out <env root> [--master-seed N] [--only substr] [--no-adversaries] [--gate-log path]
alertforge sample --workflow W --tier T --seed S --out DIR # one ad-hoc task (fresh seed)
alertforge spread --out <env root> [--episodes N] # P-mut histogram + grader timing
"""
from __future__ import annotations
import argparse
import json
import os
import random
import statistics
import sys
import time
WORKFLOWS = ["slo-onboarding", "alert-storm-cleanup", "missed-page-postmortem", "latency-slo", "team-reorg-migration"]
TIERS = ["easy", "medium", "hard"]
RELEASE = "0.1.0"
def plan(seeds=(1, 2)) -> list[tuple[str, str, str, int]]:
out, i = [], 0
for wf in WORKFLOWS:
for tier in TIERS:
for s in seeds:
i += 1
out.append((f"af-{i:03d}-{wf}-{tier}-s{s}", wf, tier, s))
return out
def _require_tools() -> None:
from .grader.promrun import tool
import shutil
for t in ("promtool", "amtool"):
if not shutil.which(tool(t)):
sys.exit(f"{t} not found: put it on PATH or set AF_{t.upper()}=/path/to/{t}")
def cmd_build(a) -> int:
from . import gate, render
_require_tools()
tasks_dir = os.path.join(a.out, "tasks")
os.makedirs(tasks_dir, exist_ok=True)
rows, failures = [], 0
log = open(a.gate_log, "a") if a.gate_log else None
for tid, wf, tier, seed in plan():
if a.only and a.only not in tid:
continue
t0 = time.time()
m = render.build_task(wf, tier, seed, tasks_dir, tid, a.master_seed)
res = gate.gate_task(m, a.out, adversaries=not a.no_adversaries)
res["build_seconds"] = round(time.time() - t0, 1)
failures += 0 if res["pass"] else 1
print(f"{tid:44s} {'PASS' if res['pass'] else 'FAIL'} oracle={res['oracle']} null={res['null']} "
f"partial={res['partial']:.3f} grader={res['grader_seconds']}s checks={m['n_checks']}"
+ ("" if res["pass"] else f"\n {res['reasons'][:3]}"), flush=True)
if log:
log.write(json.dumps({**res, "ts": time.strftime("%Y-%m-%dT%H:%M:%S")}, default=str) + "\n")
w = m["world"]
rows.append({"task_id": tid, "workflow": wf, "tier": tier, **{k: w.axes[k] for k in (
"archetype", "primary_verifier_pattern", "persona", "tone", "expertise", "rules_format")},
"services": [s.name for s in w.services], "n_requirements": len(w.reqs), "n_checks": m["n_checks"],
"req_counts": {c: sum(1 for r in w.reqs if r.category == c) for c in
("req_alerts", "req_repairs", "req_recording", "req_routing", "req_inhibit")},
"oracle_reward": res["oracle"], "partial_reward": res["partial"], "null_reward": res["null"],
"grader_seconds": res["grader_seconds"], "generator_version": RELEASE})
if not a.only:
registry = [{"name": "3ambench", "version": RELEASE,
"description": "Prometheus alerting-as-code tasks graded behaviorally by promtool/amtool.",
"tasks": [{"name": r["task_id"], "path": f"tasks/{r['task_id']}"} for r in rows],
"metrics": [{"type": "mean", "kwargs": {}}]},
{"name": "3ambench-mini", "version": RELEASE, "description": "The 5 easy seed-1 tasks.",
"tasks": [{"name": r["task_id"], "path": f"tasks/{r['task_id']}"} for r in rows
if r["tier"] == "easy" and r["task_id"].endswith("-s1")],
"metrics": [{"type": "mean", "kwargs": {}}]}]
with open(os.path.join(a.out, "registry.json"), "w") as fh:
json.dump(registry, fh, indent=2)
with open(os.path.join(a.out, "manifest.jsonl"), "w") as fh:
for r in rows:
fh.write(json.dumps(r) + "\n")
print(f"{len(rows) - failures}/{len(rows)} tasks pass the local gate")
return 1 if failures else 0
def cmd_sample(a) -> int:
from . import gate, render
_require_tools()
tid = f"af-sample-{a.workflow}-{a.tier}-s{a.seed}"
m = render.build_task(a.workflow, a.tier, a.seed, a.out, tid, a.master_seed)
res = gate.gate_task(m, None, adversaries=False)
print(json.dumps({k: res[k] for k in ("task_id", "oracle", "null", "partial", "pass")}))
return 0
def cmd_spread(a) -> int:
from . import render, solutions
_require_tools()
rng = random.Random(a.seed)
out = {"pmut": [], "ops": [], "grader_seconds": {t: [] for t in TIERS}}
tmp = os.path.join(a.out, ".spread-tmp")
for tid, wf, tier, seed in plan():
if a.only and a.only not in tid:
continue
m = render.build_task(wf, tier, seed, tmp, tid, a.master_seed)
w = m["world"]
from .grader.grade import Grader
g = Grader(m["spec"], m["hidden_dir"])
for _ in range(a.episodes):
(files, am, extra), ops = solutions.pmut(w, rng)
t0 = time.time()
k, _ = render.grade_state(w, m["spec"], m["hidden_dir"], m["static"], files, am, g, extra)
out["grader_seconds"][tier].append(time.time() - t0)
out["pmut"].append(k["reward"])
out["ops"].append({"task": tid, "ops": ops, "reward": k["reward"], "progress": k["progress"]})
print(f"{tid:44s} pmut mean={statistics.mean(x['reward'] for x in out['ops'] if x['task'] == tid):.3f}", flush=True)
xs = out["pmut"]
hist = [sum(1 for x in xs if lo <= x < lo + 0.1 or (lo == 0.9 and x == 1.0)) for lo in [i / 10 for i in range(10)]]
summary = {"n": len(xs), "mean": statistics.mean(xs), "at_zero": sum(1 for x in xs if x == 0) / len(xs),
"at_one": sum(1 for x in xs if x == 1) / len(xs), "hist_0.1": hist,
"grader_seconds_p50": {t: round(statistics.median(v), 2) for t, v in out["grader_seconds"].items() if v},
"grader_seconds_max": {t: round(max(v), 2) for t, v in out["grader_seconds"].items() if v}}
print(json.dumps(summary, indent=1))
if a.report:
with open(a.report, "w") as fh:
json.dump({"summary": summary, "episodes": out["ops"]}, fh, indent=1)
return 0
def main(argv=None) -> int:
ap = argparse.ArgumentParser(prog="alertforge")
sub = ap.add_subparsers(dest="cmd", required=True)
b = sub.add_parser("build")
b.add_argument("--out", required=True)
b.add_argument("--master-seed", type=int, default=20260928)
b.add_argument("--only")
b.add_argument("--no-adversaries", action="store_true")
b.add_argument("--gate-log")
s = sub.add_parser("sample")
for k in ("--workflow", "--tier", "--out"):
s.add_argument(k, required=True)
s.add_argument("--seed", type=int, required=True)
s.add_argument("--master-seed", type=int, default=20260928)
p = sub.add_parser("spread")
p.add_argument("--out", required=True)
p.add_argument("--episodes", type=int, default=4)
p.add_argument("--seed", type=int, default=7)
p.add_argument("--only")
p.add_argument("--report")
p.add_argument("--master-seed", type=int, default=20260928)
a = ap.parse_args(argv)
return {"build": cmd_build, "sample": cmd_sample, "spread": cmd_spread}[a.cmd](a)
if __name__ == "__main__":
sys.exit(main())
|