|
| 1 | +"""Shared baseline evaluation helpers. |
| 2 | +
|
| 3 | +Baselines emit the same `verdicts` shape consumed by `benchmarks.metrics`: |
| 4 | +
|
| 5 | + dict[case_id] = (Verdict | None, aligned_bool) |
| 6 | +
|
| 7 | +This lets base-model and tool baselines use the same scoring functions as |
| 8 | +Nullsec-S1 without changing labels or metric semantics. |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import json |
| 13 | +from datetime import datetime, timezone |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from benchmarks.metrics import ( |
| 17 | + debug_report, |
| 18 | + detection_over, |
| 19 | + failed_case_ids, |
| 20 | + false_safe_rate, |
| 21 | + hallucination_rate, |
| 22 | + owasp_coverage, |
| 23 | + patch_correctness, |
| 24 | + per_category_recall, |
| 25 | + secure_generation_score, |
| 26 | +) |
| 27 | + |
| 28 | +ROOT = Path(__file__).resolve().parents[2] |
| 29 | +REPORT_ROOT = ROOT / "benchmarks" / "reports" / "baselines" |
| 30 | + |
| 31 | +FAMILIES = { |
| 32 | + "detection_accuracy": detection_over, |
| 33 | + "false_safe_rate": false_safe_rate, |
| 34 | + "hallucination_rate": hallucination_rate, |
| 35 | + "owasp_coverage": owasp_coverage, |
| 36 | + "patch_correctness": patch_correctness, |
| 37 | + "secure_generation": secure_generation_score, |
| 38 | + "per_category_recall": per_category_recall, |
| 39 | + "failed_cases": failed_case_ids, |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +def build_suite(cases: list[dict], verdicts: dict, *, include_patch_metrics: bool = True) -> dict: |
| 44 | + suite = {} |
| 45 | + for name, fn in FAMILIES.items(): |
| 46 | + if not include_patch_metrics and name in {"patch_correctness", "secure_generation"}: |
| 47 | + suite[name] = { |
| 48 | + "not_applicable": True, |
| 49 | + "note": "baseline does not generate secure patches", |
| 50 | + } |
| 51 | + continue |
| 52 | + suite[name] = fn(cases, verdicts) |
| 53 | + |
| 54 | + dbg = debug_report(cases, verdicts, failed_only=True) |
| 55 | + suite["debug"] = dbg |
| 56 | + produced = sum(1 for c in cases if verdicts.get(c["id"], (None, False))[1]) |
| 57 | + suite["summary"] = { |
| 58 | + "total_cases": len(cases), |
| 59 | + "total_outputs": produced, |
| 60 | + "detection_f1": suite["detection_accuracy"].get("detection_f1"), |
| 61 | + "detection_precision": suite["detection_accuracy"].get("detection_precision"), |
| 62 | + "detection_recall": suite["detection_accuracy"].get("detection_recall"), |
| 63 | + "false_safe_rate": suite["false_safe_rate"].get("false_safe_rate"), |
| 64 | + "hallucination_rate": suite["hallucination_rate"].get("hallucination_rate"), |
| 65 | + "patch_correctness": suite["patch_correctness"].get("patch_correctness"), |
| 66 | + "secure_generation_score": suite["secure_generation"].get("secure_generation_score"), |
| 67 | + "owasp_coverage": suite["owasp_coverage"].get("coverage"), |
| 68 | + "categories_below_full_recall": suite["per_category_recall"].get("categories_below_full_recall"), |
| 69 | + "failed_case_ids": [f["id"] for f in suite["failed_cases"].get("failed", [])], |
| 70 | + } |
| 71 | + return suite |
| 72 | + |
| 73 | + |
| 74 | +def write_baseline_report( |
| 75 | + *, |
| 76 | + system_id: str, |
| 77 | + system_name: str, |
| 78 | + dataset: str, |
| 79 | + cases: list[dict], |
| 80 | + verdicts: dict, |
| 81 | + provenance: dict, |
| 82 | + include_patch_metrics: bool = True, |
| 83 | + coverage_limits: dict | None = None, |
| 84 | +) -> Path: |
| 85 | + suite = build_suite(cases, verdicts, include_patch_metrics=include_patch_metrics) |
| 86 | + if coverage_limits: |
| 87 | + suite["coverage_limits"] = coverage_limits |
| 88 | + report = { |
| 89 | + "benchmark": "SUITE", |
| 90 | + "system": system_name, |
| 91 | + "provenance": { |
| 92 | + "system_id": system_id, |
| 93 | + "system_name": system_name, |
| 94 | + "dataset": dataset, |
| 95 | + "generated_at": datetime.now(timezone.utc).isoformat(), |
| 96 | + **provenance, |
| 97 | + }, |
| 98 | + "results": suite, |
| 99 | + } |
| 100 | + out_dir = REPORT_ROOT / system_id |
| 101 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 102 | + out = out_dir / "SUITE.json" |
| 103 | + out.write_text(json.dumps(report, indent=2), encoding="utf-8") |
| 104 | + return out |
0 commit comments