"""Aggregate runs.jsonl into precision + token-cost stats.""" from __future__ import annotations import json import sys from pathlib import Path def compute_stats(log_path: Path) -> dict: if not log_path.exists(): return {"by_agent": {}, "by_rule": {}, "runs": 0} by_agent: dict[str, dict] = {} by_rule: dict[str, dict] = {} runs: set[str] = set() for line in log_path.read_text(encoding="utf-8").splitlines(): if not line.strip(): continue rec = json.loads(line) agent = rec.get("agent", "?") if rec["kind"] == "subagent_run": runs.add(rec["run_id"]) a = by_agent.setdefault(agent, _empty_agent()) a["tokens"] += rec.get("input_tokens", 0) + rec.get("output_tokens", 0) a["duration_ms"] += rec.get("duration_ms", 0) a["runs"] += 1 elif rec["kind"] == "verdict": verdict = rec["verdict"] a = by_agent.setdefault(agent, _empty_agent()) a["total"] += 1 a[verdict] = a.get(verdict, 0) + 1 rule_key = f"{agent}/{rec['rule_id']}" r = by_rule.setdefault(rule_key, _empty_rule()) r["total"] += 1 r[verdict] = r.get(verdict, 0) + 1 for a in by_agent.values(): a["precision"] = a["kept"] / a["total"] if a["total"] else 0.0 a["tokens_per_kept"] = a["tokens"] / a["kept"] if a["kept"] else float("inf") for r in by_rule.values(): r["precision"] = r["kept"] / r["total"] if r["total"] else 0.0 return {"by_agent": by_agent, "by_rule": by_rule, "runs": len(runs)} def _empty_agent() -> dict: return { "tokens": 0, "duration_ms": 0, "runs": 0, "total": 0, "kept": 0, "dismissed": 0, "false_positive": 0, } def _empty_rule() -> dict: return {"total": 0, "kept": 0, "dismissed": 0, "false_positive": 0} def main(argv: list[str]) -> int: log = Path.home() / ".claude/cache/audit-terraform/runs.jsonl" stats = compute_stats(log) print(json.dumps(stats, indent=2)) return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv[1:]))