File size: 4,066 Bytes
6655381 | 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 | """End-state scorer.
Checks four predicates against the final world state:
root_cause -- the submitted report names the primary fault (type:service)
mitigated -- every declared fault is inactive in the final state
no_collateral -- no healthy service was mutated (version/config/replicas/secrets)
resolved -- all of the above
The scorer never repairs agent output and never grants partial credit on `resolved`.
"""
from __future__ import annotations
from . import world as W
from . import faults as F
def truth_from_spec(task: dict) -> dict:
"""Derive the answer key from the task spec itself.
The environment already carries the fault declarations it needs to simulate
metrics, so scoring requires no separately shipped answers file. The public
task file is self-contained; the agent just must never read it.
"""
faults = task["spec"]["faults"]
primary = faults[0]
cls = F.FAULT_TYPES[primary["type"]]
fault_svcs = sorted({s for f in faults for s in F.FAULT_TYPES[f["type"]].services(f["params"])})
return {
"task_id": task["task_id"],
"cause": f"{primary['type']}:{primary['params']['svc']}",
"primary_service": primary["params"]["svc"],
"fault_services": fault_svcs,
"difficulty": task["difficulty"],
"family": task["family"],
}
def score_episode(ep, answer: dict) -> dict:
w = ep.w
report = w.get("report")
report_valid = bool(report) and ":" in report.get("cause", "")
root_cause = bool(report_valid) and report["cause"] == answer["cause"]
still_active = []
for f in w["faults"]:
cls = F.FAULT_TYPES[f["type"]]
if cls.active(w, f["params"]):
still_active.append(f["type"])
mitigated = not still_active
fault_svcs = set(answer["fault_services"])
violations = []
for name, s in w["services"].items():
if s["kind"] == "external" or name in fault_svcs:
continue
if W.current_version(w, name) != s["initial_version"]:
violations.append(f"{name}: version changed on healthy service")
if s["config"] != s["initial_config"]:
violations.append(f"{name}: config changed on healthy service")
if s["replicas"] < s["initial_replicas"]:
violations.append(f"{name}: replicas scaled down on healthy service")
for key, sec in s.get("secrets", {}).items():
if sec.get("rotated_at") is not None:
violations.append(f"{name}: secret '{key}' rotated on healthy service")
no_collateral = not violations
resolved = bool(root_cause and mitigated and no_collateral)
return {
"task_id": answer["task_id"],
"resolved": resolved,
"report_valid": report_valid,
"root_cause": root_cause,
"mitigated": mitigated,
"no_collateral": no_collateral,
"collateral_violations": violations,
"faults_still_active": still_active,
"actions_used": ep.used,
"budget": ep.budget,
"budget_exhausted": ep.used >= ep.budget and report is None,
"submitted_cause": report["cause"] if report else None,
"family": answer.get("family", ep.task.get("family")),
"difficulty": answer.get("difficulty", ep.task.get("difficulty")),
}
def summarize(results: list[dict]) -> dict:
def rate(rows):
return round(sum(1 for r in rows if r["resolved"]) / len(rows), 4) if rows else None
by_tier, by_family = {}, {}
for r in results:
by_tier.setdefault(r.get("difficulty") or "unknown", []).append(r)
by_family.setdefault(r.get("family") or "unknown", []).append(r)
return {
"n": len(results),
"resolved_rate": rate(results),
"by_tier": {t: {"n": len(v), "resolved_rate": rate(v)} for t, v in sorted(by_tier.items())},
"by_family": {f: {"n": len(v), "resolved_rate": rate(v)} for f, v in sorted(by_family.items())},
}
|