File size: 23,467 Bytes
f31910c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python3
"""Render a markdown leaderboard from one or more evaluation results.

    python -m evaluation.leaderboard --baselines            # score built-ins
    python -m evaluation.leaderboard --baselines -o LEADERBOARD.md

Models/defenses are ranked by detection rate (descending). Pass extra
predictions files with ``--predictions name=path.jsonl`` to add real entries.
"""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

from evaluation.score import (
    SEVERITY_WEIGHTS,
    EvalResult,
    ensemble_coverage,
    load_dataset,
    load_predictions,
    normalize_label,
    pairwise_mcnemar,
    residual_hard_set,
    score_predictions,
)

CATEGORY_ORDER = [
    "tool_output_injection",
    "goal_hijacking",
    "privilege_escalation",
    "data_exfiltration",
    "multi_turn_stateful",
    "mcp_context_poisoning",
    "tool_shadowing",
]

# Most-dangerous first, so the per-severity block reads left-to-right worst-case.
SEVERITY_ORDER = ["critical", "high", "medium", "low"]

# Injection surfaces, most-prevalent first β€” the untrusted channel the payload
# rides in on. Exposes which ingestion surface a detector is blind to.
SURFACE_ORDER = [
    "tool_output",
    "mcp_response",
    "api_response",
    "rag_document",
    "file_content",
]


def _short(cat: str) -> str:
    return "".join(w[0] for w in cat.split("_")).upper()


def _find_pair(paired: dict | None, name_a: str, name_b: str) -> dict | None:
    """Return the McNemar pair result for two detectors regardless of order."""
    if not paired:
        return None
    for p in paired.get("pairs", []):
        if {p["detector_a"], p["detector_b"]} == {name_a, name_b}:
            return p
    return None


def _rank_key(r: EvalResult) -> tuple:
    """Ascending sort key giving: balanced accuracy desc (the calibration-
    resistant headline), then detection rate desc, then name asc β€” a
    deterministic total order. ``nan`` metrics sort last."""
    ba = r.balanced_accuracy
    dr = r.detection_rate
    return (
        -(ba if ba == ba else -1.0),
        -(dr if dr == dr else -1.0),
        r.name,
    )


def render_leaderboard(
    results: list[EvalResult],
    frontier: dict | None = None,
    ensemble: dict | None = None,
    paired: dict | None = None,
) -> str:
    has_benign = any(r.n_safe for r in results)
    ranked = sorted(results, key=_rank_key)

    lines: list[str] = []
    lines.append("# AgentInjectionBench Leaderboard")
    lines.append("")
    fingerprints = {r.dataset_fingerprint for r in results if r.dataset_fingerprint}
    if len(fingerprints) == 1:
        lines.append(f"Dataset SHA-256: `{next(iter(fingerprints))}`")
        lines.append("")
    if has_benign:
        lines.append(
            "Ranked by **balanced accuracy** = mean of detection rate (recall on "
            "attacks) and specificity (recall on the benign control split). A "
            "flag-everything defense scores 50% here, not 100% β€” the benign "
            "controls make the leaderboard calibration-resistant."
        )
        lines.append("")
        lines.append("**MCC** (Matthews correlation) folds all four confusion cells into one "
                     "score in [βˆ’1, +1]; like balanced accuracy it pins a trivial "
                     "flag-everything / flag-nothing detector at 0, but it also penalises "
                     "low precision, so it is the most honest single-number summary under the "
                     f"{ranked[0].n_unsafe}-attack / {ranked[0].n_safe}-benign class imbalance. "
                     "Detection = attacks caught; FPR = benign wrongly flagged (lower is better); "
                     "Precision = of everything flagged, the share that was a real attack; "
                     "Sev-Wtd Det = detection rate weighted by severity (critical counts most). "
                     "**95% CI** is the Wilson-score interval on balanced accuracy, and "
                     "**MCC 95% CI** its bootstrap counterpart β€” MCC is non-linear in the four "
                     "confusion cells, so it takes a resampling interval rather than a closed-form "
                     "one β€” with only "
                     f"{ranked[0].n_unsafe} attacks and {ranked[0].n_safe} benign controls, "
                     "adjacent ranks whose intervals overlap are not statistically distinguishable.")
        lines.append("")
        lines.append("| Rank | Model / Defense | Balanced Acc | 95% CI | MCC | MCC 95% CI | Detection | Sev-Wtd Det | FPR | Precision | F1 | Attacks | Benign |")
        lines.append("|---:|:---|---:|:---:|---:|:---:|---:|---:|---:|---:|---:|---:|---:|")
        for i, r in enumerate(ranked, 1):
            fpr = f"{r.false_positive_rate:.1%}" if r.n_safe else "β€”"
            prec = f"{r.precision:.1%}" if (r.n_detected + r.n_false_positive) else "β€”"
            f1 = f"{r.f1:.3f}" if r.f1 == r.f1 else "β€”"
            swd = f"{r.severity_weighted_detection:.1%}" if r.n_unsafe else "β€”"
            mcc = f"{r.mcc:+.3f}" if r.mcc == r.mcc else "β€”"
            mcc_lo, mcc_hi = r.mcc_ci
            mcc_ci_txt = f"{mcc_lo:+.2f}–{mcc_hi:+.2f}" if mcc_lo == mcc_lo else "β€”"
            ba_lo, ba_hi = r.balanced_accuracy_ci
            ci = f"{ba_lo:.0%}–{ba_hi:.0%}" if ba_lo == ba_lo else "β€”"
            lines.append(
                f"| {i} | {r.name} | {r.balanced_accuracy:.1%} | {ci} | {mcc} | {mcc_ci_txt} | {r.detection_rate:.1%} | "
                f"{swd} | {fpr} | {prec} | {f1} | {r.n_unsafe} | {r.n_safe} |"
            )
        # Is the #1 vs #2 gap real, or within sampling noise? Compare their
        # balanced-accuracy CIs β€” overlapping intervals mean the lead is not
        # statistically established at this sample size.
        if len(ranked) >= 2:
            top, second = ranked[0], ranked[1]
            t_lo, t_hi = top.balanced_accuracy_ci
            s_lo, s_hi = second.balanced_accuracy_ci
            if t_lo == t_lo and s_lo == s_lo:
                overlap = t_lo <= s_hi and s_lo <= t_hi
                lines.append("")
                if overlap:
                    lines.append(
                        f"> ⚠️ **Ranking caveat:** the top two entries "
                        f"(`{top.name}`, `{second.name}`) have **overlapping** "
                        f"balanced-accuracy 95% CIs ({t_lo:.0%}–{t_hi:.0%} vs. "
                        f"{s_lo:.0%}–{s_hi:.0%}), so the #1 lead is **within sampling "
                        f"noise** β€” not yet statistically established. A larger dataset "
                        f"(the v0.2 goal) would tighten these intervals."
                    )
                else:
                    lines.append(
                        f"> βœ… **Ranking note:** `{top.name}`'s balanced-accuracy 95% CI "
                        f"({t_lo:.0%}–{t_hi:.0%}) does **not** overlap the runner-up's "
                        f"({s_lo:.0%}–{s_hi:.0%}), so its lead is statistically distinguishable "
                        f"at this sample size."
                    )
            # Paired McNemar test on the identical sample set β€” the correct,
            # more-powerful complement to the unpaired CI-overlap check above.
            top_pair = _find_pair(paired, top.name, second.name) if paired else None
            if top_pair is not None:
                lines.append("")
                if top_pair["n_discordant"] == 0:
                    lines.append(
                        f"> πŸ”¬ **Paired check (McNemar):** `{top.name}` and `{second.name}` "
                        f"agree on every sample (no discordant pairs), so they are "
                        f"indistinguishable on this dataset."
                    )
                elif top_pair["significant"]:
                    lines.append(
                        f"> πŸ”¬ **Paired check (McNemar, {top_pair['method'].replace('_', ' ')}):** "
                        f"on the identical sample set the two differ **significantly** "
                        f"(p = {top_pair['p_value']:.3f}; {top_pair['a_correct_b_wrong']} samples "
                        f"only `{top_pair['detector_a']}` gets right vs. "
                        f"{top_pair['a_wrong_b_correct']} only `{top_pair['detector_b']}` does), "
                        f"favouring `{top_pair['better']}`."
                    )
                else:
                    lines.append(
                        f"> πŸ”¬ **Paired check (McNemar, {top_pair['method'].replace('_', ' ')}):** "
                        f"even paired on the identical sample set, the #1 lead is **not** "
                        f"significant (p = {top_pair['p_value']:.3f}; discordant split "
                        f"{top_pair['a_correct_b_wrong']} vs. {top_pair['a_wrong_b_correct']}) β€” "
                        f"the ranking is within sampling noise."
                    )
    else:
        lines.append(
            "Detection rate = fraction of injection attacks correctly flagged. "
            "Attack-success rate (ASR) = attacks that slipped through (lower is better)."
        )
        lines.append("")
        lines.append("| Rank | Model / Defense | Detection Rate | Sev-Wtd Det | ASR | Attacks |")
        lines.append("|---:|:---|---:|---:|---:|---:|")
        for i, r in enumerate(ranked, 1):
            swd = f"{r.severity_weighted_detection:.1%}" if r.n_unsafe else "β€”"
            lines.append(
                f"| {i} | {r.name} | {r.detection_rate:.1%} | {swd} | "
                f"{r.attack_success_rate:.1%} | {r.n_unsafe} |"
            )
    lines.append("")

    # Per-category detection-rate matrix.
    cats = [c for c in CATEGORY_ORDER if any(c in r.by_category for r in ranked)]
    # include any category not in the canonical order
    for r in ranked:
        for c in r.by_category:
            if c not in cats:
                cats.append(c)

    lines.append("## Per-category detection rate")
    lines.append("")
    legend = ", ".join(f"`{_short(c)}` = {c}" for c in cats)
    lines.append(legend)
    lines.append("")
    header = "| Model / Defense | " + " | ".join(_short(c) for c in cats) + " |"
    sep = "|:---|" + "|".join(["---:"] * len(cats)) + "|"
    lines.append(header)
    lines.append(sep)
    for r in ranked:
        cells = []
        for c in cats:
            g = r.by_category.get(c)
            cells.append(f"{g.detection_rate:.0%}" if g else "β€”")
        lines.append(f"| {r.name} | " + " | ".join(cells) + " |")
    lines.append("")

    # Per-severity detection-rate matrix β€” exposes the "catches the easy stuff,
    # misses the dangerous stuff" failure mode a flat detection rate hides.
    sevs = [s for s in SEVERITY_ORDER if any(s in r.by_severity for r in ranked)]
    for r in ranked:
        for s in r.by_severity:
            if s not in sevs:
                sevs.append(s)

    lines.append("## Per-severity detection rate")
    lines.append("")
    lines.append(
        "Weighted into **Sev-Wtd Det** above with weights "
        + ", ".join(f"{s}={SEVERITY_WEIGHTS[s]}" for s in SEVERITY_ORDER if s in SEVERITY_WEIGHTS)
        + " β€” a detector that catches only low-severity attacks scores low here even at a decent flat rate."
    )
    lines.append("")
    header = "| Model / Defense | " + " | ".join(s.capitalize() for s in sevs) + " |"
    sep = "|:---|" + "|".join(["---:"] * len(sevs)) + "|"
    lines.append(header)
    lines.append(sep)
    for r in ranked:
        cells = []
        for s in sevs:
            g = r.by_severity.get(s)
            cells.append(f"{g.detection_rate:.0%}" if g else "β€”")
        lines.append(f"| {r.name} | " + " | ".join(cells) + " |")
    lines.append("")

    # Per-injection-surface detection-rate matrix β€” which untrusted ingestion
    # channel (tool output, MCP response, RAG document, …) a detector is blind
    # to, independent of attack category.
    surfaces = [s for s in SURFACE_ORDER if any(s in r.by_surface for r in ranked)]
    for r in ranked:
        for s in r.by_surface:
            if s not in surfaces:
                surfaces.append(s)

    if surfaces:
        lines.append("## Per-injection-surface detection rate")
        lines.append("")
        lines.append(
            "The untrusted channel the payload arrives on. A low cell means the "
            "detector rarely inspects that surface β€” e.g. a scanner that reads only "
            "conversation text is blind to a poisoned tool definition or a retrieved "
            "document, regardless of the attack category."
        )
        lines.append("")
        header = "| Model / Defense | " + " | ".join(s.replace("_", " ") for s in surfaces) + " |"
        sep = "|:---|" + "|".join(["---:"] * len(surfaces)) + "|"
        lines.append(header)
        lines.append(sep)
        for r in ranked:
            cells = []
            for s in surfaces:
                g = r.by_surface.get(s)
                cells.append(f"{g.detection_rate:.0%}" if g else "β€”")
            lines.append(f"| {r.name} | " + " | ".join(cells) + " |")
        lines.append("")

    # Ensemble coverage β€” the OR-ensemble ceiling and the greedy minimal set that
    # reaches it. The complement of the residual hard set: what the best
    # *combination* of baselines catches, and at what false-positive cost. Only
    # meaningful with β‰₯2 informative detectors.
    if ensemble and len(ensemble.get("detectors", [])) >= 2:
        u = ensemble["union"]
        lines.append("## Ensemble coverage β€” the best any combination can do")
        lines.append("")
        lines.append(
            f"Flagging a sample when **any** of the {len(ensemble['detectors'])} discriminating "
            f"detectors flags it (an OR-ensemble) is the detection ceiling of the current "
            f"baselines β€” but it accumulates every member's false positives, so the honest "
            f"ceiling is a **detection / FPR pair**. The union catches "
            f"**{u['detection_rate']:.1%}** of attacks at **{u['false_positive_rate']:.1%}** FPR "
            f"(balanced accuracy {u['balanced_accuracy']:.1%}, MCC {u['mcc']:+.3f})."
        )
        excluded = ensemble.get("excluded_detectors") or []
        if excluded:
            lines.append("")
            lines.append(
                "> Constant-prediction anchors ("
                + ", ".join(f"`{n}`" for n in excluded)
                + ") are excluded: a flag-everything anchor would hit 100% detection at 100% "
                "FPR and a flag-nothing anchor adds nothing, so neither informs a "
                "best-real-combination analysis."
            )
        lines.append("")
        lines.append(
            "**Greedy minimal set** β€” add the detector that newly catches the most "
            "so-far-missed attacks (ties broken by smaller added FPR):"
        )
        lines.append("")
        lines.append("| Step | + Detector | New attacks | Cumulative detection | Cumulative FPR |")
        lines.append("|---:|:---|---:|---:|---:|")
        for i, step in enumerate(ensemble["greedy"], 1):
            lines.append(
                f"| {i} | `{step['detector']}` | +{step['marginal_attacks_caught']} | "
                f"{step['cumulative_detection_rate']:.1%} | "
                f"{step['cumulative_false_positive_rate']:.1%} |"
            )
        lines.append("")
        n_used = len(ensemble["greedy"])
        n_inf = len(ensemble["detectors"])
        if n_used < n_inf:
            lines.append(
                f"_Just **{n_used} of {n_inf}** detectors reach the full union detection "
                f"ceiling β€” the rest add no attack the others miss._"
            )
            lines.append("")

    # Residual hard set β€” the attacks that evade *every* detector at once. Only
    # meaningful with β‰₯2 detectors: with one, "missed by all" is just its own
    # detection rate, already in the table above.
    if frontier and frontier.get("n_detectors", 0) >= 2:
        n_ev = frontier["n_evaded_by_all"]
        n_at = frontier["n_attacks"]
        rate = frontier["evasion_rate"]
        lines.append("## Residual hard set β€” attacks no baseline catches")
        lines.append("")
        lines.append(
            f"Of {n_at} attacks, **{n_ev} ({rate:.1%})** are missed by **all "
            f"{frontier['n_detectors']} discriminating detectors simultaneously** β€” the "
            "frontier this benchmark exists to push. An attack caught by *some* detector "
            "is within reach of the right ensemble; one evaded by *every* baseline is the "
            "open problem the next detector or attack category must target."
        )
        excluded = frontier.get("excluded_detectors") or []
        if excluded:
            lines.append("")
            lines.append(
                "> Constant-prediction anchors ("
                + ", ".join(f"`{n}`" for n in excluded)
                + ") are excluded: a flag-everything / flag-nothing detector carries no "
                "information for this analysis and would trivially collapse it."
            )
        lines.append("")
        if n_ev:
            lines.append("| Attack category | Evaded by all | Injection surface | Evaded by all |")
            lines.append("|:---|---:|:---|---:|")
            cat_rows = sorted(frontier["by_category"].items(), key=lambda kv: (-kv[1], kv[0]))
            surf_rows = sorted(frontier["by_surface"].items(), key=lambda kv: (-kv[1], kv[0]))
            for i in range(max(len(cat_rows), len(surf_rows))):
                c = f"{cat_rows[i][0]} | {cat_rows[i][1]}" if i < len(cat_rows) else " | "
                s = f"{surf_rows[i][0].replace('_', ' ')} | {surf_rows[i][1]}" if i < len(surf_rows) else " | "
                lines.append(f"| {c} | {s} |")
            lines.append("")
            ids = frontier["sample_ids"]
            shown = ids[:15]
            id_txt = ", ".join(f"`{i}`" for i in shown)
            more = f" _(+{len(ids) - len(shown)} more)_" if len(ids) > len(shown) else ""
            lines.append(f"**Unanimously-evaded sample ids:** {id_txt}{more}")
            lines.append("")
        else:
            lines.append(
                "_Every attack is caught by at least one baseline β€” no residual hard "
                "set at this dataset size._"
            )
            lines.append("")

    # Pairwise paired-significance matrix β€” the correct test for "is detector X
    # really better than Y", conditioning on the samples where exactly one is
    # right. Complements the per-detector table (unpaired) above.
    if paired and len(paired.get("pairs", [])) >= 1:
        lines.append("## Paired significance β€” McNemar's test")
        lines.append("")
        lines.append(
            "The table above ranks detectors by balanced accuracy with **unpaired** "
            "Wilson intervals. But every detector is scored on the **same samples**, so "
            "the sharper question β€” *is X really better than Y?* β€” is a **paired** one. "
            "McNemar's test conditions on the **discordant** samples (where exactly one "
            "detector is correct) and asks whether the split is more lopsided than a coin "
            "flip. Concordant samples (both right / both wrong) carry no signal and are "
            "ignored. p-values use the **exact binomial** test when discordant pairs are "
            "few (≀ 25) and the **continuity-corrected chi-square** otherwise; "
            "**bold** p-values are significant at Ξ± = 0.05."
        )
        lines.append("")
        lines.append(
            "> ⚠️ McNemar compares **overall accuracy**, so under this benchmark's "
            f"{ranked[0].n_unsafe}-attack / {ranked[0].n_safe}-benign imbalance a constant *flag-everything* anchor can win a "
            "pair on raw accuracy alone β€” which is exactly why the headline ranking uses "
            "**balanced accuracy** and **MCC** (both pin that anchor at chance). Read this "
            "table as the paired significance of differences **between the real detectors**, "
            "a complement to β€” not a replacement for β€” the calibration-resistant ranking above."
        )
        lines.append("")
        lines.append(
            "| Detector A | Detector B | A-only right | B-only right | p-value | Method | Verdict |"
        )
        lines.append("|:---|:---|---:|---:|---:|:---|:---|")
        for p in paired["pairs"]:
            pv = f"**{p['p_value']:.3f}**" if p["significant"] else f"{p['p_value']:.3f}"
            if p["n_discordant"] == 0:
                verdict = "identical"
            elif p["significant"]:
                verdict = f"`{p['better']}` better"
            else:
                verdict = "within noise"
            method = {
                "exact_binomial": "exact",
                "chi2_continuity": "χ² (cc)",
                "no_discordant": "β€”",
            }.get(p["method"], p["method"])
            lines.append(
                f"| `{p['detector_a']}` | `{p['detector_b']}` | {p['a_correct_b_wrong']} | "
                f"{p['a_wrong_b_correct']} | {pv} | {method} | {verdict} |"
            )
        lines.append("")

    lines.append(
        "_Generated by `python -m evaluation.leaderboard`. "
        "Baselines are dependency-free reference defenses, not strong guardrails._"
    )
    lines.append("")
    return "\n".join(lines)


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--baselines", action="store_true", help="Score all built-in detectors")
    parser.add_argument(
        "--predictions",
        action="append",
        default=[],
        metavar="name=path.jsonl",
        help="Add a predictions file as a named entry (repeatable)",
    )
    parser.add_argument("-o", "--output", type=Path, help="Write to a file instead of stdout")
    args = parser.parse_args(argv)

    samples = load_dataset()
    results: list[EvalResult] = []
    predictions_by_detector: dict[str, dict[str, str]] = {}

    if args.baselines:
        from evaluation.detectors import DETECTORS

        for name in DETECTORS:
            preds = {s["id"]: normalize_label(DETECTORS[name](s)) for s in samples}
            predictions_by_detector[name] = preds
            results.append(score_predictions(samples, preds, name=name))

    for spec in args.predictions:
        if "=" not in spec:
            parser.error(f"--predictions expects name=path, got {spec!r}")
        name, _, path = spec.partition("=")
        preds = load_predictions(Path(path))
        predictions_by_detector[name] = preds
        results.append(score_predictions(samples, preds, name=name))

    if not results:
        parser.error("Nothing to score: pass --baselines and/or --predictions")

    frontier = residual_hard_set(samples, predictions_by_detector)
    ensemble = ensemble_coverage(samples, predictions_by_detector)
    paired = pairwise_mcnemar(samples, predictions_by_detector) if len(results) >= 2 else None
    md = render_leaderboard(results, frontier=frontier, ensemble=ensemble, paired=paired)
    if args.output:
        args.output.write_text(md)
        print(f"Wrote {args.output} ({len(results)} entries)")
    else:
        sys.stdout.write(md)
    return 0


if __name__ == "__main__":
    sys.exit(main())