File size: 25,493 Bytes
26bf1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
"""

CounterFeint β€” Before/After Evaluation Lane.



This module provides a reproducible, held-out eval harness that compares two

Investigator policies ("before training" vs "after training") on a fixed set

of (task_id, seed) tuples which are **distinct** from any seeds used during

training. The outputs are the three artefacts the README's headline section

needs:



    eval_results.json   β€” full per-episode metrics for every (tag, task, seed)

    eval_summary.md     β€” markdown delta table, human-readable

    eval_plot.png       β€” bar-chart visualisation (matplotlib, dev-only)



Per-episode metrics tracked:



* ``grader_score``            β€” Investigator triage/calibration score from

                                 :func:`counterfeint.graders.grade_episode`

* ``track_a_score``           β€” reasoning-audit score from the Auditor's

                                 submitted :class:`AuditReport`

* ``track_b_score``           β€” Fraudster plausibility score (sanity signal:

                                 higher = realistic adversary)

* ``n_fraud_leaks``           β€” false approvals on ground-truth fraud ads

                                 (the single most visible failure mode)

* ``budget_used_pct``         β€” fraction of the Investigator's action budget

                                 actually consumed (efficiency proxy)

* ``fallback_count``          β€” LLM β†’ scripted fallback count for the

                                 Investigator in this episode (0 means the

                                 LLM answered every turn cleanly)

* ``rewards_by_role``         β€” raw cumulative rewards per role



Design notes

------------



* **Factories, not instances.**  The caller provides zero-argument callables

  that construct a fresh Investigator (and optional Fraudster / Auditor).

  This guarantees clean per-episode state β€” especially important for the

  :class:`counterfeint.agents.base.LLMPolicyBase` ``fallback_count``.

* **Live server required.**  The eval sweep drives real episodes via

  :func:`counterfeint.inference.run_three_agent_episode`, so the CounterFeint

  FraudArena server must be running at ``COUNTERFEINT_ENV_URL``.

* **Sweep modes.**  The primary sweep uses :class:`ReactiveFraudster` (stable,

  headline numbers).  The caller can swap in :class:`LLMFraudster` for a

  harder adversarial-curriculum eval, or set ``use_real_world_ads=True`` to

  route through the Meta-CIB-modeled holdout set (see

  :mod:`counterfeint.data.real_world_loader`) once that asset lands.

"""

from __future__ import annotations

import json
import logging
import os
import sys
import time
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional

try:
    from .data.real_world_loader import (
        HoldoutAd,
        count_by_ring,
        list_case_studies,
        load_real_world_holdout,
    )
    from .inference import ENV_URL, run_three_agent_episode
    from .scripted import HeuristicAuditor, ReactiveFraudster, ScriptedInvestigator
except ImportError:  # pragma: no cover - script execution fallback
    sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
    from counterfeint.data.real_world_loader import (  # type: ignore[no-redef]
        HoldoutAd,
        count_by_ring,
        list_case_studies,
        load_real_world_holdout,
    )
    from counterfeint.inference import ENV_URL, run_three_agent_episode  # type: ignore[no-redef]
    from counterfeint.scripted import (  # type: ignore[no-redef]
        HeuristicAuditor,
        ReactiveFraudster,
        ScriptedInvestigator,
    )

logger = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Public constants
# ---------------------------------------------------------------------------


EVAL_SEEDS: Dict[str, List[int]] = {
    "task_1": list(range(1001, 1011)),       # 1001..1010
    "task_2": list(range(2001, 2011)),       # 2001..2010
    "task_3": list(range(3001, 3011)),       # 3001..3010
    "task_3_unseen": list(range(4001, 4006)),  # 4001..4005 β€” held-out budget regime
}
"""Held-out seeds β€” 10 per training-tier task, plus 5 generalisation seeds

on ``task_3_unseen``.  All seeds are deliberately disjoint from the training

seed range (which uses seed=42 for the scripted baseline and small integers

for self-play rollouts).  ``task_3_unseen`` shares the template universe of

``task_3`` but ships a tighter action budget (~1.2/ad) and one extra ring,

so a model that over-fit to ``task_3``'s budget distribution should regress

visibly on these seeds.  Five seeds (vs ten on the training-tier tasks)

keeps the eval-loop wallclock from doubling for what is purely a

generalisation probe.



Judges can reproduce any eval number with

``python -m counterfeint.eval_suite --seed 1001 --task task_1``.

"""


PolicyFactory = Callable[[], Any]
"""Zero-arg callable returning a fresh Policy (one per episode)."""


# ---------------------------------------------------------------------------
# Metric dataclasses
# ---------------------------------------------------------------------------


@dataclass
class EpisodeMetrics:
    """Flat, JSON-serialisable record of a single eval episode."""

    tag: str
    task_id: str
    seed: int
    grader_score: float
    track_a_score: float
    track_b_score: float
    n_fraud_leaks: int
    n_ground_truth_fraud: int
    budget_used_pct: float
    fallback_count: int
    steps: int
    end_reason: Optional[str]
    rewards_by_role: Dict[str, float] = field(default_factory=dict)
    error: Optional[str] = None

    def to_dict(self) -> Dict[str, Any]:
        return asdict(self)


@dataclass
class AggregatedMetrics:
    """Averages over the 10 episodes of a single task (under one tag)."""

    tag: str
    task_id: str
    n_episodes: int
    grader_score_mean: float
    track_a_score_mean: float
    n_fraud_leaks_mean: float
    budget_used_pct_mean: float
    fallback_count_total: int
    errors: int

    def to_dict(self) -> Dict[str, Any]:
        return asdict(self)


# ---------------------------------------------------------------------------
# Metric extraction (pure, unit-testable)
# ---------------------------------------------------------------------------


def _parse_episode_metrics(

    tag: str,

    task_id: str,

    seed: int,

    episode_result: Dict[str, Any],

) -> EpisodeMetrics:
    """Distill a ``run_three_agent_episode`` return dict into flat metrics.



    Pure function β€” no I/O, no server calls. Kept separate from

    :func:`run_eval` so it can be exercised directly from unit tests

    without spinning up a live environment.

    """
    final_state = episode_result.get("final_state") or {}
    audit_report = final_state.get("audit_report") or {}

    track_a_score = float(audit_report.get("investigator_audit_score", 1.0))
    track_b_score = float(audit_report.get("fraudster_plausibility_score", 1.0))

    investigator_state = final_state.get("investigator_state") or {}
    verdicts = investigator_state.get("verdicts") or {}
    n_fraud_leaks = 0
    n_ground_truth_fraud = 0
    for v in verdicts.values():
        ground_truth = v.get("ground_truth")
        if ground_truth == "fraud":
            n_ground_truth_fraud += 1
            if v.get("verdict") == "approve":
                n_fraud_leaks += 1

    total_ads = int(investigator_state.get("total_ads") or 0)
    remaining_budget = int(investigator_state.get("remaining_budget") or 0)
    budget_used_pct = 0.0
    if total_ads > 0 and remaining_budget >= 0:
        # action_budget per task is proportional to total_ads; we express
        # budget consumption as "1 - remaining/total" as a stable proxy.
        budget_used_pct = max(0.0, min(1.0, 1.0 - (remaining_budget / max(total_ads, 1))))

    fallback_counts = episode_result.get("fallback_counts") or {}
    fallback_count = int(fallback_counts.get("investigator", 0))

    return EpisodeMetrics(
        tag=tag,
        task_id=task_id,
        seed=seed,
        grader_score=float(episode_result.get("grader_score", 0.0)),
        track_a_score=track_a_score,
        track_b_score=track_b_score,
        n_fraud_leaks=n_fraud_leaks,
        n_ground_truth_fraud=n_ground_truth_fraud,
        budget_used_pct=budget_used_pct,
        fallback_count=fallback_count,
        steps=int(episode_result.get("steps", 0)),
        end_reason=episode_result.get("end_reason"),
        rewards_by_role=dict(episode_result.get("rewards_by_role") or {}),
        error=episode_result.get("error"),
    )


def _aggregate_per_task(

    tag: str,

    task_id: str,

    episodes: List[EpisodeMetrics],

) -> AggregatedMetrics:
    """Compute mean-over-episodes for the summary markdown + plot."""
    valid = [m for m in episodes if m.error is None]
    n = len(valid)
    if n == 0:
        return AggregatedMetrics(
            tag=tag,
            task_id=task_id,
            n_episodes=0,
            grader_score_mean=0.0,
            track_a_score_mean=0.0,
            n_fraud_leaks_mean=0.0,
            budget_used_pct_mean=0.0,
            fallback_count_total=sum(m.fallback_count for m in episodes),
            errors=len(episodes) - n,
        )
    return AggregatedMetrics(
        tag=tag,
        task_id=task_id,
        n_episodes=n,
        grader_score_mean=sum(m.grader_score for m in valid) / n,
        track_a_score_mean=sum(m.track_a_score for m in valid) / n,
        n_fraud_leaks_mean=sum(m.n_fraud_leaks for m in valid) / n,
        budget_used_pct_mean=sum(m.budget_used_pct for m in valid) / n,
        fallback_count_total=sum(m.fallback_count for m in episodes),
        errors=len(episodes) - n,
    )


# ---------------------------------------------------------------------------
# Core sweep
# ---------------------------------------------------------------------------


def run_eval(

    *,

    tag: str,

    investigator_factory: PolicyFactory,

    fraudster_factory: PolicyFactory = lambda: ReactiveFraudster(seed=42),

    auditor_factory: PolicyFactory = lambda: HeuristicAuditor(),

    seeds: Optional[Dict[str, List[int]]] = None,

    env_base_url: str = ENV_URL,

    max_steps: int = 200,

    log: bool = False,

) -> Dict[str, List[EpisodeMetrics]]:
    """Run the held-out sweep under a single named policy configuration.



    Parameters

    ----------

    tag

        Short string tagging this run in outputs (e.g. ``"before"``,

        ``"after_grpo_v1"``).  Shown in the summary table and plot legend.

    investigator_factory

        Zero-arg callable building the Investigator to evaluate.

    fraudster_factory, auditor_factory

        Zero-arg callables for the opponent and auditor. Default to the

        scripted ``ReactiveFraudster`` / ``HeuristicAuditor`` so eval

        numbers are stable across runs.

    seeds

        Override :data:`EVAL_SEEDS`. Useful for a smoke test with a

        single seed per task.

    env_base_url

        CounterFeint server URL. Default: ``COUNTERFEINT_ENV_URL`` env

        var or ``http://localhost:8000``.

    max_steps, log

        Forwarded to :func:`run_three_agent_episode`.



    Returns

    -------

    dict

        ``{task_id: [EpisodeMetrics, ...]}`` with one entry per seed.

    """
    seeds = seeds or EVAL_SEEDS
    results: Dict[str, List[EpisodeMetrics]] = {}

    for task_id, task_seeds in seeds.items():
        task_metrics: List[EpisodeMetrics] = []
        for seed in task_seeds:
            logger.info(
                "[%s] running %s seed=%d (fraudster=%s, investigator=%s)...",
                tag,
                task_id,
                seed,
                type(fraudster_factory()).__name__,
                type(investigator_factory()).__name__,
            )
            try:
                episode_result = run_three_agent_episode(
                    task_id,
                    seed=seed,
                    env_base_url=env_base_url,
                    max_steps=max_steps,
                    fraudster_policy=fraudster_factory(),
                    investigator_policy=investigator_factory(),
                    auditor_policy=auditor_factory(),
                    log=log,
                )
            except Exception as exc:  # noqa: BLE001 - eval must never raise
                logger.exception(
                    "[%s] %s seed=%d raised: %s", tag, task_id, seed, exc
                )
                episode_result = {
                    "task_id": task_id,
                    "grader_score": 0.0,
                    "steps": 0,
                    "end_reason": "eval_exception",
                    "rewards_by_role": {},
                    "fallback_counts": {},
                    "final_state": {},
                    "error": str(exc),
                }

            metrics = _parse_episode_metrics(tag, task_id, seed, episode_result)
            task_metrics.append(metrics)
            logger.info(
                "[%s] %s seed=%d score=%.3f leaks=%d fallbacks=%d",
                tag,
                task_id,
                seed,
                metrics.grader_score,
                metrics.n_fraud_leaks,
                metrics.fallback_count,
            )

        results[task_id] = task_metrics

    return results


# ---------------------------------------------------------------------------
# Before/After artefact writers
# ---------------------------------------------------------------------------


def summarize_real_world_holdout() -> Dict[str, Any]:
    """Summary of the Meta-CIB-modeled holdout dataset (no opt-in needed).



    Pulls counts via :func:`counterfeint.data.real_world_loader.count_by_ring`

    so we can render "evaluated against N synthetic ads grounded in M

    Meta CIB case studies" without ever loading the ad text itself.

    Suitable for embedding in :func:`run_before_after` outputs and the

    README "Evaluated against Meta-CIB-modeled ads" subsection.

    """
    counts = count_by_ring()
    case_studies = list_case_studies()
    return {
        "n_ads_total": sum(counts.values()),
        "n_case_studies": len(case_studies),
        "case_studies": case_studies,
        "ads_per_case_study": counts,
    }


def _write_eval_json(

    before: Dict[str, List[EpisodeMetrics]],

    after: Dict[str, List[EpisodeMetrics]],

    before_tag: str,

    after_tag: str,

    path: Path,

    *,

    holdout_summary: Optional[Dict[str, Any]] = None,

) -> None:
    payload = {
        "schema": "counterfeint.eval_suite.v1",
        "generated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        "tags": {"before": before_tag, "after": after_tag},
        "before": {
            task_id: [m.to_dict() for m in metrics]
            for task_id, metrics in before.items()
        },
        "after": {
            task_id: [m.to_dict() for m in metrics]
            for task_id, metrics in after.items()
        },
    }
    if holdout_summary is not None:
        payload["real_world_holdout"] = holdout_summary
    path.write_text(json.dumps(payload, indent=2), encoding="utf-8")


def _format_delta(delta: float, *, higher_is_better: bool = True) -> str:
    sign = "+" if delta >= 0 else ""
    marker = ""
    if higher_is_better and delta > 0:
        marker = " ↑"
    elif higher_is_better and delta < 0:
        marker = " ↓"
    elif not higher_is_better and delta < 0:
        marker = " ↑"  # lower is better, so a decrease is an improvement
    elif not higher_is_better and delta > 0:
        marker = " ↓"
    return f"{sign}{delta:.3f}{marker}"


def _write_eval_summary_md(

    before_agg: Dict[str, AggregatedMetrics],

    after_agg: Dict[str, AggregatedMetrics],

    before_tag: str,

    after_tag: str,

    path: Path,

) -> None:
    """Render a human-readable delta table to ``eval_summary.md``."""
    lines: List[str] = []
    lines.append(f"# CounterFeint Eval: `{before_tag}` β†’ `{after_tag}`")
    lines.append("")
    lines.append(
        f"Held-out sweep across {len(EVAL_SEEDS)} tasks Γ— 10 seeds each, "
        f"evaluated against `ReactiveFraudster` (stable adversary)."
    )
    lines.append("")

    lines.append(
        "| Task | Metric | "
        f"{before_tag} | {after_tag} | Delta |"
    )
    lines.append("|------|--------|--------|-------|-------|")

    task_ids = list(before_agg.keys() or after_agg.keys())
    for task_id in task_ids:
        b = before_agg.get(task_id)
        a = after_agg.get(task_id)
        if b is None or a is None:
            continue

        def _row(label: str, b_val: float, a_val: float, *, higher_is_better: bool = True) -> str:
            delta = a_val - b_val
            return (
                f"| {task_id} | {label} | "
                f"{b_val:.3f} | {a_val:.3f} | "
                f"{_format_delta(delta, higher_is_better=higher_is_better)} |"
            )

        lines.append(_row("grader_score (↑)", b.grader_score_mean, a.grader_score_mean))
        lines.append(_row("track_a_score (↑)", b.track_a_score_mean, a.track_a_score_mean))
        lines.append(
            _row(
                "n_fraud_leaks (↓)",
                b.n_fraud_leaks_mean,
                a.n_fraud_leaks_mean,
                higher_is_better=False,
            )
        )
        lines.append(
            _row(
                "budget_used_pct (↓)",
                b.budget_used_pct_mean,
                a.budget_used_pct_mean,
                higher_is_better=False,
            )
        )

    lines.append("")
    if any(a.fallback_count_total for a in after_agg.values()):
        lines.append("### Fallback activity (LLM β†’ scripted, per tag/task)")
        lines.append("")
        lines.append(f"| Task | {before_tag} fallbacks | {after_tag} fallbacks |")
        lines.append("|------|----------------------|----------------------|")
        for task_id in task_ids:
            b = before_agg.get(task_id)
            a = after_agg.get(task_id)
            if b is None or a is None:
                continue
            lines.append(
                f"| {task_id} | {b.fallback_count_total} | {a.fallback_count_total} |"
            )

    path.write_text("\n".join(lines) + "\n", encoding="utf-8")


def _write_eval_plot(

    before_agg: Dict[str, AggregatedMetrics],

    after_agg: Dict[str, AggregatedMetrics],

    before_tag: str,

    after_tag: str,

    path: Path,

) -> None:
    """Render a 2x2 bar-chart PNG comparing the four headline metrics.



    matplotlib is a **dev-only** dependency (see ``requirements-dev.txt``)

    to keep the runtime Docker image slim. If matplotlib is not available,

    this writes a small text stub next to ``path`` instead of raising.

    """
    try:
        import matplotlib

        matplotlib.use("Agg")
        import matplotlib.pyplot as plt  # noqa: WPS433 - lazy import by design
    except ImportError:
        stub = path.with_suffix(".txt")
        stub.write_text(
            "matplotlib not installed β€” install requirements-dev.txt to generate the PNG.\n",
            encoding="utf-8",
        )
        logger.warning("matplotlib unavailable; wrote stub to %s", stub)
        return

    task_ids = list(before_agg.keys() or after_agg.keys())
    if not task_ids:
        logger.warning("No tasks to plot; skipping %s", path)
        return

    n_tasks = len(task_ids)
    fig, axes = plt.subplots(2, 2, figsize=(10, 7))
    fig.suptitle(
        f"CounterFeint Eval: {before_tag} vs {after_tag}\n"
        f"{n_tasks} tasks Γ— 10 held-out seeds"
    )

    metric_specs = [
        ("grader_score_mean", "Grader score (↑ better)", axes[0][0]),
        ("track_a_score_mean", "Track A reasoning (↑)", axes[0][1]),
        ("n_fraud_leaks_mean", "Mean fraud leaks (↓)", axes[1][0]),
        ("budget_used_pct_mean", "Budget used pct (↓)", axes[1][1]),
    ]

    for attr, title, ax in metric_specs:
        before_vals = [getattr(before_agg[t], attr) for t in task_ids]
        after_vals = [getattr(after_agg[t], attr) for t in task_ids]
        x = list(range(n_tasks))
        width = 0.35
        ax.bar([i - width / 2 for i in x], before_vals, width, label=before_tag)
        ax.bar([i + width / 2 for i in x], after_vals, width, label=after_tag)
        ax.set_title(title)
        ax.set_xticks(x)
        ax.set_xticklabels(task_ids)
        ax.legend(fontsize="x-small")
        ax.grid(True, axis="y", linestyle="--", alpha=0.4)

    fig.tight_layout(rect=(0, 0, 1, 0.94))
    fig.savefig(path, dpi=150)
    plt.close(fig)
    logger.info("Wrote %s", path)


def run_before_after(

    *,

    before_tag: str,

    after_tag: str,

    before_investigator_factory: PolicyFactory,

    after_investigator_factory: PolicyFactory,

    fraudster_factory: PolicyFactory = lambda: ReactiveFraudster(seed=42),

    auditor_factory: PolicyFactory = lambda: HeuristicAuditor(),

    out_dir: Path,

    seeds: Optional[Dict[str, List[int]]] = None,

    env_base_url: str = ENV_URL,

    include_real_world_summary: bool = True,

) -> Dict[str, Any]:
    """Run the full before/after comparison and write all three artefacts.



    Writes:



    * ``{out_dir}/eval_results.json``

    * ``{out_dir}/eval_summary.md``

    * ``{out_dir}/eval_plot.png``  (stub .txt if matplotlib is missing)



    Returns the aggregated metrics dict for programmatic use (e.g. the

    final cell of the Colab training notebook).

    """
    out_dir.mkdir(parents=True, exist_ok=True)

    logger.info("=== Running BEFORE sweep: %s ===", before_tag)
    before = run_eval(
        tag=before_tag,
        investigator_factory=before_investigator_factory,
        fraudster_factory=fraudster_factory,
        auditor_factory=auditor_factory,
        seeds=seeds,
        env_base_url=env_base_url,
    )

    logger.info("=== Running AFTER sweep: %s ===", after_tag)
    after = run_eval(
        tag=after_tag,
        investigator_factory=after_investigator_factory,
        fraudster_factory=fraudster_factory,
        auditor_factory=auditor_factory,
        seeds=seeds,
        env_base_url=env_base_url,
    )

    before_agg = {
        task_id: _aggregate_per_task(before_tag, task_id, eps)
        for task_id, eps in before.items()
    }
    after_agg = {
        task_id: _aggregate_per_task(after_tag, task_id, eps)
        for task_id, eps in after.items()
    }

    holdout_summary: Optional[Dict[str, Any]] = None
    if include_real_world_summary:
        try:
            holdout_summary = summarize_real_world_holdout()
        except Exception as exc:  # noqa: BLE001
            logger.warning("Could not summarise holdout dataset: %s", exc)

    json_path = out_dir / "eval_results.json"
    md_path = out_dir / "eval_summary.md"
    png_path = out_dir / "eval_plot.png"

    _write_eval_json(
        before,
        after,
        before_tag,
        after_tag,
        json_path,
        holdout_summary=holdout_summary,
    )
    _write_eval_summary_md(before_agg, after_agg, before_tag, after_tag, md_path)
    _write_eval_plot(before_agg, after_agg, before_tag, after_tag, png_path)

    logger.info("Wrote %s, %s, %s", json_path, md_path, png_path)

    return {
        "before": {t: m.to_dict() for t, m in before_agg.items()},
        "after": {t: m.to_dict() for t, m in after_agg.items()},
        "real_world_holdout": holdout_summary,
        "out_dir": str(out_dir),
    }


# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------


def _default_out_dir() -> Path:
    return Path(os.environ.get("COUNTERFEINT_EVAL_OUT_DIR", "eval_outputs"))


def main() -> None:  # pragma: no cover - thin CLI glue
    import argparse

    logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")

    parser = argparse.ArgumentParser(
        description="CounterFeint before/after eval sweep (scripted vs scripted by default)."
    )
    parser.add_argument("--out-dir", type=Path, default=_default_out_dir())
    parser.add_argument("--before-tag", default="scripted")
    parser.add_argument("--after-tag", default="scripted_rerun")
    parser.add_argument(
        "--smoke",
        action="store_true",
        help="Run one seed per task for a quick smoke test.",
    )
    args = parser.parse_args()

    seeds: Optional[Dict[str, List[int]]] = None
    if args.smoke:
        seeds = {task: [seeds_list[0]] for task, seeds_list in EVAL_SEEDS.items()}

    run_before_after(
        before_tag=args.before_tag,
        after_tag=args.after_tag,
        before_investigator_factory=lambda: ScriptedInvestigator(),
        after_investigator_factory=lambda: ScriptedInvestigator(),
        out_dir=args.out_dir,
        seeds=seeds,
    )


if __name__ == "__main__":  # pragma: no cover
    main()