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 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Render the #66 mitigation before/after comparison export.

The source of truth is the paper-grade post-PR175 mitigation cohort summary plus
the tracked raw benchmark artifacts. This keeps
``results/metrics/mitigation_before_after.csv`` as the stable paper-facing
comparison path without preserving superseded post-PR180 diagnostic rows.
"""

from __future__ import annotations

import csv
import json
from collections import Counter
from datetime import datetime
from pathlib import Path
from typing import Any

ROOT = Path(__file__).resolve().parents[1]
METRICS_DIR = ROOT / "results" / "metrics"
SOURCE_SUMMARY = METRICS_DIR / "gcp_post175_mitigation_4tier_summary.csv"
SCORE_SOURCE = METRICS_DIR / "scenario_scores.jsonl"
OUTPUT_CSV = METRICS_DIR / "mitigation_before_after.csv"

FIELDNAMES = [
    "lane",
    "phase",
    "run_name",
    "cell",
    "orchestration_mode",
    "mcp_mode",
    "model_id",
    "slurm_job_id",
    "git_sha",
    "scenario_set_name",
    "scenario_set_hash",
    "experiment_family",
    "experiment_cell",
    "wandb_run_url",
    "benchmark_config_path",
    "benchmark_summary_path",
    "host_name",
    "gpu_type",
    "run_status",
    "finished_at",
    "scenarios_attempted",
    "scenarios_completed",
    "success_rate",
    "failure_count",
    "wall_clock_seconds_total",
    "latency_seconds_mean",
    "latency_seconds_p50",
    "latency_seconds_p95",
    "mcp_latency_seconds_mean",
    "mcp_latency_seconds_p95",
    "tool_latency_seconds_mean",
    "tool_call_count_total",
    "tool_call_count_mean",
    "tool_error_count",
    "input_tokens_total",
    "output_tokens_total",
    "tokens_per_second_mean",
    "judge_score_mean",
    "judge_score_p50",
    "judge_score_p95",
    "judge_score_p5",
    "judge_pass_rate",
    "benchmark_run_dir",
    "profiling_dir",
    "torch_profile_dir",
    "replay_dir",
    "profiling_summary",
    "profiling_gpu_util_mean",
    "profiling_gpu_util_max",
    "profiling_gpu_mem_used_mib_mean",
    "profiling_gpu_mem_used_mib_max",
    "profiling_power_draw_w_mean",
    "profiling_power_draw_w_max",
    "mitigation_name",
    "mitigation_enabled",
    "mitigation_guard_triggered",
    "mitigation_guard_blocked_final_answer",
    "mitigation_guard_blocked_work_order",
    "repair_attempt_count",
    "repair_success_rate",
    "supported_success_after_repair_rate",
    "fault_risk_adjudication_decision",
    "comparison_status",
    "notes",
]

PHASE_BY_ROW_GROUP = {
    "YS_BASELINE": "baseline",
    "YS_GUARD": "guard",
    "YS_REPAIR": "repair",
    "YS_ADJ": "adjudication",
    "ZS_BASELINE": "baseline",
    "ZS_GUARD": "guard",
    "ZS_REPAIR": "repair",
    "ZS_ADJ": "adjudication",
}

MITIGATION_BY_PHASE = {
    "baseline": "baseline",
    "guard": "missing_evidence_final_answer_guard",
    "repair": "missing_evidence_retry_replan_guard",
    "adjudication": "explicit_fault_risk_adjudication_step",
}

DISPLAY_CELL_BY_LANE = {
    "YS": "PE-S-M",
    "ZS": "V-S-M",
}

ORDER = {
    "YS_BASELINE": 0,
    "YS_GUARD": 1,
    "YS_REPAIR": 2,
    "YS_ADJ": 3,
    "ZS_BASELINE": 4,
    "ZS_GUARD": 5,
    "ZS_REPAIR": 6,
    "ZS_ADJ": 7,
}

HOST_NAME = "smartgrid-a100-ixqt-uswest3-20260503-2116"
GPU_TYPE = "NVIDIA A100-SXM4-40GB"
SCENARIO_SET_NAME = "mitigation15_4tier"
COMPARISON_STATUS = "paper_grade_post175_matched"


def fmt(value: Any, digits: int = 6) -> str:
    if value is None or value == "":
        return ""
    if isinstance(value, bool):
        return str(value).lower()
    if isinstance(value, int):
        return str(value)
    if isinstance(value, float):
        return f"{value:.{digits}f}".rstrip("0").rstrip(".")
    return str(value)


def percentile(values: list[float], q: float) -> float | None:
    if not values:
        return None
    ordered = sorted(values)
    if len(ordered) == 1:
        return ordered[0]
    pos = (len(ordered) - 1) * q
    low = int(pos)
    high = min(low + 1, len(ordered) - 1)
    frac = pos - low
    return ordered[low] * (1 - frac) + ordered[high] * frac


def load_json(path: Path) -> dict[str, Any]:
    with path.open() as f:
        return json.load(f)


def load_scores_by_run() -> dict[str, list[dict[str, Any]]]:
    scores: dict[str, list[dict[str, Any]]] = {}
    with SCORE_SOURCE.open() as f:
        for line in f:
            row = json.loads(line)
            scores.setdefault(row["run_name"], []).append(row)
    return scores


def load_latencies(run_dir: Path) -> list[float]:
    latencies_path = run_dir / "latencies.jsonl"
    values: list[float] = []
    with latencies_path.open() as f:
        for line in f:
            row = json.loads(line)
            values.append(float(row["latency_seconds"]))
    return values


def iter_trial_json(run_dir: Path) -> list[Path]:
    return sorted(run_dir.glob("*_run[0-9][0-9].json"))


def wall_clock_seconds(meta: dict[str, Any]) -> float | None:
    started = meta.get("started_at")
    finished = meta.get("finished_at")
    if not started or not finished:
        return None
    start_dt = datetime.fromisoformat(started)
    finish_dt = datetime.fromisoformat(finished)
    return (finish_dt - start_dt).total_seconds()


def response_has_error(response: Any) -> bool:
    if isinstance(response, dict):
        return bool(response.get("error"))
    return False


def aggregate_trials(run_dir: Path) -> dict[str, Any]:
    tool_calls = 0
    tool_errors = 0
    guard = Counter()
    repair_records = 0
    repair_triggered = 0
    repair_attempts = 0
    repair_successes = 0
    repaired_successful_trials = 0
    adjudication_decisions: Counter[str] = Counter()

    for path in iter_trial_json(run_dir):
        data = load_json(path)
        for step in data.get("history") or []:
            tool_calls += 1
            if (
                step.get("success") is False
                or step.get("executor_success") is False
                or step.get("error")
                or response_has_error(step.get("response"))
            ):
                tool_errors += 1

        guard_payload = data.get("mitigation_guard") or {}
        if guard_payload:
            guard["records"] += 1
            for key in ("triggered", "blocked_final_answer", "blocked_work_order"):
                if guard_payload.get(key):
                    guard[key] += 1

        repair_payload = data.get("mitigation_repair") or {}
        if repair_payload:
            repair_records += 1
            if repair_payload.get("triggered"):
                repair_triggered += 1
            attempts = repair_payload.get("attempts") or []
            repair_attempts += len(attempts)
            if repair_payload.get("repaired"):
                repair_successes += 1
                if data.get("success"):
                    repaired_successful_trials += 1

        adjudication_payload = data.get("fault_risk_adjudication") or {}
        if adjudication_payload:
            decision = adjudication_payload.get("decision") or "unknown"
            adjudication_decisions[decision] += 1

    return {
        "tool_call_count_total": tool_calls,
        "tool_error_count": tool_errors,
        "mitigation_guard_triggered": guard.get("triggered", 0),
        "mitigation_guard_blocked_final_answer": guard.get("blocked_final_answer", 0),
        "mitigation_guard_blocked_work_order": guard.get("blocked_work_order", 0),
        "repair_records": repair_records,
        "repair_triggered": repair_triggered,
        "repair_attempt_count": repair_attempts,
        "repair_success_count": repair_successes,
        "repaired_successful_trials": repaired_successful_trials,
        "fault_risk_adjudication_decision": ";".join(
            f"{key}={adjudication_decisions[key]}"
            for key in sorted(adjudication_decisions)
        ),
    }


def validate_count(label: str, actual: int, expected: str, field: str) -> None:
    expected_int = int(expected)
    if actual != expected_int:
        raise ValueError(
            f"{label}: {field} count mismatch, expected {expected_int}, got {actual}"
        )


def validate_close(
    label: str,
    actual: float,
    expected: str,
    field: str,
    tolerance: float = 5e-6,
) -> None:
    if expected == "":
        return
    expected_float = float(expected)
    if abs(actual - expected_float) > tolerance:
        raise ValueError(
            f"{label}: {field} mismatch, expected {expected_float}, got {actual}"
        )


def judge_metrics(scores: list[dict[str, Any]]) -> dict[str, Any]:
    score_values = [float(row["score_6d"]) for row in scores]
    pass_count = sum(1 for row in scores if bool(row["pass"]))
    return {
        "judge_pass_count": pass_count,
        "judge_pass_rate": pass_count / len(scores),
        "judge_score_mean": sum(score_values) / len(score_values),
        "judge_score_p50": percentile(score_values, 0.50),
        "judge_score_p95": percentile(score_values, 0.95),
        "judge_score_p5": percentile(score_values, 0.05),
    }


def collect_judge_metrics(
    source_rows: list[dict[str, str]],
    scores_by_run: dict[str, list[dict[str, Any]]],
) -> dict[str, dict[str, Any]]:
    metrics_by_label: dict[str, dict[str, Any]] = {}
    for source_row in source_rows:
        label = source_row["row_group"]
        scores = scores_by_run.get(source_row["run_name"], [])
        validate_count(label, len(scores), source_row["judge_count"], "judge")
        metrics = judge_metrics(scores)
        validate_count(
            label,
            metrics["judge_pass_count"],
            source_row["judge_pass_count"],
            "judge pass",
        )
        validate_close(
            label,
            metrics["judge_pass_rate"],
            source_row["judge_pass_rate"],
            "judge pass rate",
        )
        validate_close(
            label,
            metrics["judge_score_mean"],
            source_row["judge_score_mean"],
            "judge score mean",
        )
        validate_close(
            label,
            metrics["judge_score_p50"],
            source_row["judge_score_p50"],
            "judge score p50",
        )
        metrics_by_label[label] = metrics

    for source_row in source_rows:
        label = source_row["row_group"]
        baseline_label = source_row.get("baseline_row_group")
        if not baseline_label or baseline_label == label:
            continue
        metrics = metrics_by_label[label]
        baseline = metrics_by_label[baseline_label]
        pass_lift = metrics["judge_pass_rate"] - baseline["judge_pass_rate"]
        score_lift = metrics["judge_score_mean"] - baseline["judge_score_mean"]
        validate_close(
            label,
            pass_lift,
            source_row["pass_rate_lift_vs_baseline"],
            "pass-rate lift vs baseline",
        )
        validate_close(
            label,
            score_lift,
            source_row["score_mean_lift_vs_baseline"],
            "score-mean lift vs baseline",
        )
        metrics["pass_rate_lift_vs_baseline"] = pass_lift
        metrics["score_mean_lift_vs_baseline"] = score_lift

    return metrics_by_label


def build_row(
    source_row: dict[str, str],
    metrics_by_label: dict[str, dict[str, Any]],
) -> dict[str, str]:
    label = source_row["row_group"]
    run_name = source_row["run_name"]
    run_dir = ROOT / source_row["run_dir"]
    meta = load_json(run_dir / "meta.json")
    latencies = load_latencies(run_dir)
    trials = iter_trial_json(run_dir)

    validate_count(label, len(trials), source_row["raw_json_count"], "raw JSON")
    validate_count(label, len(latencies), source_row["latency_count"], "latency")

    aggregates = aggregate_trials(run_dir)
    metrics = metrics_by_label[label]
    lane = label.split("_", 1)[0]
    phase = PHASE_BY_ROW_GROUP[label]
    attempted = int(meta.get("total_runs") or source_row["raw_json_count"])
    completed = int(meta.get("pass") or 0)
    failed = int(meta.get("fail") or (attempted - completed))

    repair_success_rate = ""
    supported_success_after_repair_rate = ""
    if phase in {"repair", "adjudication"}:
        repair_triggered = aggregates["repair_triggered"]
        if repair_triggered:
            repair_success_rate = fmt(
                aggregates["repair_success_count"] / repair_triggered
            )
        supported_success_after_repair_rate = fmt(
            aggregates["repaired_successful_trials"] / attempted
        )

    notes_parts = [
        f"label={label}",
        f"cohort={source_row['cohort']}",
        f"batch={source_row['batch_prefix']}",
        f"raw_json_count={source_row['raw_json_count']}",
        f"latency_count={source_row['latency_count']}",
        f"judge_count={source_row['judge_count']}",
    ]
    if source_row.get("baseline_row_group") and phase != "baseline":
        notes_parts.append(f"baseline_row_group={source_row['baseline_row_group']}")
    if "pass_rate_lift_vs_baseline" in metrics:
        notes_parts.append(
            f"pass_rate_lift_vs_baseline={fmt(metrics['pass_rate_lift_vs_baseline'])}"
        )
    if "score_mean_lift_vs_baseline" in metrics:
        notes_parts.append(
            f"score_mean_lift_vs_baseline={fmt(metrics['score_mean_lift_vs_baseline'])}"
        )

    row = {field: "" for field in FIELDNAMES}
    row.update(
        {
            "lane": lane,
            "phase": phase,
            "run_name": run_name,
            "cell": DISPLAY_CELL_BY_LANE[lane],
            "orchestration_mode": meta.get("orchestration_mode", ""),
            "mcp_mode": meta.get("mcp_mode", ""),
            "model_id": source_row["model_id"],
            "git_sha": source_row["git_sha"],
            "scenario_set_name": SCENARIO_SET_NAME,
            "experiment_family": meta.get("experiment_family", ""),
            "experiment_cell": meta.get("experiment_cell", ""),
            "wandb_run_url": meta.get("wandb_run_url", ""),
            "benchmark_config_path": meta.get("benchmark_config_path", ""),
            "benchmark_summary_path": meta.get("benchmark_summary_path", ""),
            "host_name": HOST_NAME,
            "gpu_type": GPU_TYPE,
            "run_status": meta.get("run_status", ""),
            "finished_at": meta.get("finished_at", ""),
            "scenarios_attempted": fmt(attempted),
            "scenarios_completed": fmt(completed),
            "success_rate": fmt(completed / attempted if attempted else None),
            "failure_count": fmt(failed),
            "wall_clock_seconds_total": fmt(wall_clock_seconds(meta), 2),
            "latency_seconds_mean": fmt(sum(latencies) / len(latencies), 4),
            "latency_seconds_p50": fmt(percentile(latencies, 0.50), 4),
            "latency_seconds_p95": fmt(percentile(latencies, 0.95), 4),
            "tool_call_count_total": fmt(aggregates["tool_call_count_total"]),
            "tool_call_count_mean": fmt(
                aggregates["tool_call_count_total"] / attempted if attempted else None
            ),
            "tool_error_count": fmt(aggregates["tool_error_count"]),
            "judge_score_mean": fmt(metrics["judge_score_mean"]),
            "judge_score_p50": fmt(metrics["judge_score_p50"], 4),
            "judge_score_p95": fmt(metrics["judge_score_p95"], 4),
            "judge_score_p5": fmt(metrics["judge_score_p5"], 4),
            "judge_pass_rate": fmt(metrics["judge_pass_rate"]),
            "benchmark_run_dir": source_row["run_dir"],
            "mitigation_name": MITIGATION_BY_PHASE[phase],
            "mitigation_enabled": fmt(phase != "baseline"),
            "mitigation_guard_triggered": (
                fmt(aggregates["mitigation_guard_triggered"])
                if phase != "baseline"
                else ""
            ),
            "mitigation_guard_blocked_final_answer": (
                fmt(aggregates["mitigation_guard_blocked_final_answer"])
                if phase != "baseline"
                else ""
            ),
            "mitigation_guard_blocked_work_order": (
                fmt(aggregates["mitigation_guard_blocked_work_order"])
                if phase != "baseline"
                else ""
            ),
            "repair_attempt_count": (
                fmt(aggregates["repair_attempt_count"])
                if phase in {"repair", "adjudication"}
                else ""
            ),
            "repair_success_rate": repair_success_rate,
            "supported_success_after_repair_rate": supported_success_after_repair_rate,
            "fault_risk_adjudication_decision": (
                aggregates["fault_risk_adjudication_decision"]
                if phase == "adjudication"
                else ""
            ),
            "comparison_status": COMPARISON_STATUS,
            "notes": "; ".join(notes_parts),
        }
    )
    return row


def main() -> None:
    scores_by_run = load_scores_by_run()
    with SOURCE_SUMMARY.open() as f:
        source_rows = sorted(csv.DictReader(f), key=lambda row: ORDER[row["row_group"]])

    metrics_by_label = collect_judge_metrics(source_rows, scores_by_run)
    rows = [build_row(row, metrics_by_label) for row in source_rows]

    with OUTPUT_CSV.open("w", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDNAMES, lineterminator="\n")
        writer.writeheader()
        writer.writerows(rows)

    print(f"Wrote {OUTPUT_CSV.relative_to(ROOT)} ({len(rows)} rows)")


if __name__ == "__main__":
    main()