File size: 26,895 Bytes
d606d10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hand-audit support for the post-PR175 failure taxonomy (#194).

PR #193 produced `results/metrics/failure_taxonomy_current.csv` with
46 rows flagged `audit_status='stratified_sample'` (one per non-empty
`(experiment_cell, auto_taxonomy_label)` stratum across paper-eligible
failures). This script is the audit-side companion: it joins each
sample row to its judge log, extracts the agent trajectory + judge
verdict, and emits a per-row brief the auditor reads to decide:

    audit_decision         confirmed | relabel_suggested | evidence_thin
    audit_note             one-sentence concrete observation
    berkeley_label         specification | inter_agent_orchestration | task_verification
    failure_stage          planning | tool_selection | tool_execution | verification | final_answer
    audit_decision_source  "<reviewer>:<YYYY-MM-DD>"

Subcommands:

    briefs       — emit per-row briefs to stdout or --out for the auditor.
                   By default reads both `stratified_sample` and
                   `manual_confirmed` rows; filter via `--status`.
    add-columns  — merge a hand-edited audit-decisions sidecar TSV into
                   the CSV (defaults to `data/audit/issue194_decisions.tsv`,
                   which IS committed so the audit is reproducible from
                   inputs). Sets `audit_status` to `manual_confirmed` for
                   audited rows; idempotent on re-runs. Validates
                   audit_decision / berkeley_label / failure_stage
                   against the canonical vocab AND
                   audit_decision_source against `<handle>:<YYYY-MM-DD>`.
                   Fails loudly on unmatched keys or zero updates.
    render       — write the Markdown audit summary by reading the
                   augmented CSV plus
                   `data/audit/issue194_recurring_patterns.json` (also
                   committed) for the methodology paragraph, the
                   Berkeley-label tie-break rule, the failure-stage
                   rule (separate from the Berkeley rule per
                   `docs/failure_taxonomy_evidence.md:139-145` vs
                   `:149-151`), the recurring-patterns section, and
                   the v2->v3->v4 tie-break-relabel callout.
                   Reproducible: re-running render over committed
                   inputs is byte-identical to the committed doc.

Mirrors `scripts/audit_failure_evidence.py` (PR #189) but targets the
new failure_taxonomy_current.csv schema instead of the preliminary
PR #151 35-row table.
"""

from __future__ import annotations

import argparse
import csv
import json
import os
import re
import sys
from collections import Counter
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
TAXONOMY_CSV = REPO_ROOT / "results" / "metrics" / "failure_taxonomy_current.csv"
JUDGE_LOGS_ROOT = REPO_ROOT / "results" / "judge_logs"
DECISIONS_TSV = REPO_ROOT / "data" / "audit" / "issue194_decisions.tsv"
PATTERNS_JSON = REPO_ROOT / "data" / "audit" / "issue194_recurring_patterns.json"


def _long(p: Path) -> str:
    """Return a Windows long-path-prefixed string for file ops.

    Some `run_name` values push the absolute judge-log path past Windows'
    260-character MAX_PATH limit, which makes `Path.exists()` and
    `Path.read_text()` silently report not-found. The `\\\\?\\` prefix
    bypasses MAX_PATH on Win32 file APIs. POSIX is unaffected.
    """
    if os.name != "nt":
        return str(p)
    s = str(p.resolve())
    return s if s.startswith("\\\\?\\") else "\\\\?\\" + s


def _exists(p: Path) -> bool:
    return os.path.exists(_long(p))


def _read_text(p: Path, encoding: str = "utf-8") -> str:
    with open(_long(p), encoding=encoding) as f:
        return f.read()


AUDIT_COLUMNS = [
    "audit_decision",
    "berkeley_label",
    "failure_stage",
    "audit_note",
    "audit_decision_source",
]

BERKELEY_LABELS = {
    "specification",
    "inter_agent_orchestration",
    "task_verification",
}
FAILURE_STAGES = {
    "planning",
    "tool_selection",
    "tool_execution",
    "verification",
    "final_answer",
}
AUDIT_DECISIONS = {"confirmed", "relabel_suggested", "evidence_thin"}

# `audit_decision_source` should be `<reviewer-handle>:<YYYY-MM-DD>` per the
# audit-tooling docstring. Without validation, typos like `reviewer:2026-5-7`
# silently merge and break later programmatic provenance walks.
AUDIT_SOURCE_PATTERN = re.compile(r"^[a-z][a-z0-9_]*:\d{4}-\d{2}-\d{2}$")


def _judge_log_path(run_name: str, scenario_id: str, trial_index: str) -> Path:
    """Locate the judge log file for one (run, scenario, trial)."""
    pad = f"{int(trial_index):02d}" if str(trial_index).isdigit() else trial_index
    return JUDGE_LOGS_ROOT / run_name / f"{scenario_id}_run{pad}_judge_log.json"


def _load_taxonomy_rows() -> tuple[list[str], list[dict]]:
    with TAXONOMY_CSV.open(encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f)
        rows = list(reader)
    return list(rows[0].keys()), rows


def _extract_tool_call_summary(prompt_user: str) -> list[str]:
    """Pull the agent's tool-call sequence out of the judge prompt.

    The judge prompt embeds the trajectory as a JSON array under
    `## Agent Trajectory (tool calls and results)`. We don't need a
    perfect parse — a one-line summary per tool call (name + a hint
    of the arguments) is enough for an audit brief.
    """
    marker = "## Agent Trajectory (tool calls and results)"
    if marker not in prompt_user:
        return []
    after = prompt_user.split(marker, 1)[1]
    end_markers = [
        "## Agent Final Answer",
        "## Final Answer",
        "## Agent Answer",
        "Evaluate across",
    ]
    end_idx = len(after)
    for em in end_markers:
        i = after.find(em)
        if i != -1 and i < end_idx:
            end_idx = i
    body = after[:end_idx].strip()
    # Try to parse as JSON; if that fails, do a permissive name-extract.
    try:
        traj = json.loads(body)
    except Exception:
        # Permissive fallback: find every `"name": "..."`.
        out = []
        i = 0
        while True:
            i = body.find('"name":', i)
            if i == -1:
                break
            j = body.find('"', i + 7)
            k = body.find('"', j + 1)
            if j == -1 or k == -1:
                break
            out.append(body[j + 1 : k])
            i = k + 1
        return out

    out = []
    if isinstance(traj, list):
        for turn in traj:
            for tc in (turn or {}).get("tool_calls", []) or []:
                name = tc.get("name", "?")
                args = tc.get("arguments", {}) or {}
                arg_hint = (
                    ", ".join(f"{k}={args[k]!r}" for k in list(args)[:2])
                    if isinstance(args, dict)
                    else ""
                )
                out.append(f"{name}({arg_hint})" if arg_hint else name)
    return out


def _extract_final_answer(prompt_user: str, raw_response: str) -> str:
    """Best-effort extract of the agent's final answer from the prompt."""
    for marker in ("## Agent Final Answer", "## Final Answer", "## Agent Answer"):
        if marker in prompt_user:
            after = prompt_user.split(marker, 1)[1]
            end = after.find("Evaluate across")
            return (after[:end] if end != -1 else after).strip()[:1500]
    return ""


def _row_brief(row: dict) -> dict:
    """Build a compact audit brief for one taxonomy row.

    Returns a dict with the source row plus extracted judge-log signals.
    Missing judge log -> brief flagged `judge_log_present=False`. Uses
    `_exists` / `_read_text` so Windows long paths work the same as POSIX.
    """
    log_path = _judge_log_path(row["run_name"], row["scenario_id"], row["trial_index"])
    present = _exists(log_path)
    out = {
        "source_row": row,
        "judge_log_path": (
            str(log_path.relative_to(REPO_ROOT).as_posix()) if present else None
        ),
        "judge_log_present": present,
        "tool_calls": [],
        "final_answer": "",
        "judge_suggestions": "",
        "judge_parsed_dims": {},
    }
    if not present:
        return out
    log = json.loads(_read_text(log_path))
    out["tool_calls"] = _extract_tool_call_summary(log.get("prompt_user", ""))
    out["final_answer"] = _extract_final_answer(
        log.get("prompt_user", ""), log.get("raw_response", "")
    )
    out["judge_suggestions"] = (log.get("parsed_dims") or {}).get("suggestions", "")
    out["judge_parsed_dims"] = {
        k: v for k, v in (log.get("parsed_dims") or {}).items() if k != "suggestions"
    }
    return out


def cmd_briefs(args: argparse.Namespace) -> int:
    fields, rows = _load_taxonomy_rows()
    statuses = (
        {args.status} if args.status else {"stratified_sample", "manual_confirmed"}
    )
    sample = [r for r in rows if r["audit_status"] in statuses]
    if args.cell:
        sample = [r for r in sample if r["experiment_cell"] == args.cell]
    if args.label:
        sample = [r for r in sample if r["auto_taxonomy_label"] == args.label]
    out_lines: list[str] = []
    for r in sample:
        b = _row_brief(r)
        sr = b["source_row"]
        out_lines.append(
            f"=== {sr['run_name']} / {sr['scenario_id']} run{int(sr['trial_index']):02d} ==="
        )
        out_lines.append(
            f"cell={sr['experiment_cell']}  orch={sr['orchestration_mode']}  "
            f"model={sr['model_id']}  judge={sr['judge_model']}"
        )
        out_lines.append(
            f"auto_taxonomy_label={sr['auto_taxonomy_label']}  "
            f"failed_dims=({sr['failed_dim_count']}) {sr['failed_dims']}"
        )
        if not b["judge_log_present"]:
            out_lines.append("  [WARN] judge log MISSING at expected path")
            out_lines.append("")
            continue
        out_lines.append(f"  judge_log: {b['judge_log_path']}")
        out_lines.append(f"  parsed_dims: {b['judge_parsed_dims']}")
        if b["tool_calls"]:
            out_lines.append(
                f"  tool_calls ({len(b['tool_calls'])}): "
                + ", ".join(b["tool_calls"][:8])
            )
            if len(b["tool_calls"]) > 8:
                out_lines.append(f"    ... and {len(b['tool_calls']) - 8} more")
        else:
            out_lines.append("  tool_calls: (none extracted)")
        fa = b["final_answer"].strip().replace("\n", " ")[:240]
        if fa:
            out_lines.append(f"  final_answer: {fa}")
        sug = (b["judge_suggestions"] or "").strip().replace("\n", " ")[:300]
        if sug:
            out_lines.append(f"  judge_suggestion: {sug}")
        out_lines.append("")

    text = "\n".join(out_lines)
    if args.out:
        Path(args.out).write_text(text, encoding="utf-8")
        print(f"wrote {args.out}: {len(sample)} briefs", file=sys.stderr)
    else:
        sys.stdout.write(text)
    return 0


def cmd_add_columns(args: argparse.Namespace) -> int:
    """Merge an audit-decisions sidecar TSV/JSONL into the taxonomy CSV.

    Sidecar shape (TSV):
        run_name<TAB>scenario_id<TAB>trial_index<TAB>audit_decision
        <TAB>berkeley_label<TAB>failure_stage<TAB>audit_note
        <TAB>audit_decision_source

    Default sidecar path is `data/audit/issue194_decisions.tsv` (committed
    so the audit is reproducible from inputs); override with --decisions.

    Eligible rows: `audit_status` is `stratified_sample` (initial pass)
    or `manual_confirmed` (re-merge from the same sidecar after the
    initial flip). Matching keys are `(run_name, scenario_id,
    int(trial_index))`. After merge, `audit_status` is set to
    `manual_confirmed` (idempotent on re-runs). The other 1,920+ rows
    are left alone.

    Fails if the sidecar references a key that doesn't match any
    eligible row, or if zero rows would be updated — silent no-op
    re-runs were a v1 review-finding hazard.
    """
    fields, rows = _load_taxonomy_rows()
    if any(c not in fields for c in AUDIT_COLUMNS):
        # Insert the new columns just after audit_status for grouping.
        idx = fields.index("audit_status") + 1
        for new_col in reversed(AUDIT_COLUMNS):
            if new_col not in fields:
                fields.insert(idx, new_col)
        for r in rows:
            for new_col in AUDIT_COLUMNS:
                r.setdefault(new_col, "")

    # Load sidecar.
    sidecar = Path(args.decisions) if args.decisions else DECISIONS_TSV
    decisions: dict[tuple[str, str, int], dict[str, str]] = {}
    with sidecar.open(encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f, delimiter="\t")
        for row in reader:
            key = (row["run_name"], row["scenario_id"], int(row["trial_index"]))
            decisions[key] = row

    eligible_statuses = {"stratified_sample", "manual_confirmed"}
    n_updated = 0
    matched_keys: set[tuple[str, str, int]] = set()
    for r in rows:
        if r["audit_status"] not in eligible_statuses:
            continue
        key = (r["run_name"], r["scenario_id"], int(r["trial_index"]))
        d = decisions.get(key)
        if not d:
            continue
        if d["audit_decision"] not in AUDIT_DECISIONS:
            raise ValueError(
                f"invalid audit_decision {d['audit_decision']!r} for {key}; "
                f"must be one of {sorted(AUDIT_DECISIONS)}"
            )
        if d.get("berkeley_label") and d["berkeley_label"] not in BERKELEY_LABELS:
            raise ValueError(
                f"invalid berkeley_label {d['berkeley_label']!r} for {key}; "
                f"must be one of {sorted(BERKELEY_LABELS)}"
            )
        if d.get("failure_stage") and d["failure_stage"] not in FAILURE_STAGES:
            raise ValueError(
                f"invalid failure_stage {d['failure_stage']!r} for {key}; "
                f"must be one of {sorted(FAILURE_STAGES)}"
            )
        src = d.get("audit_decision_source") or ""
        if src and not AUDIT_SOURCE_PATTERN.match(src):
            raise ValueError(
                f"invalid audit_decision_source {src!r} for {key}; expected "
                f"`<reviewer-handle>:<YYYY-MM-DD>` (lowercase handle, ISO date)"
            )
        for col in AUDIT_COLUMNS:
            r[col] = d.get(col, "") or ""
        r["audit_status"] = "manual_confirmed"
        matched_keys.add(key)
        n_updated += 1

    unmatched = set(decisions.keys()) - matched_keys
    if unmatched:
        raise ValueError(
            f"sidecar has {len(unmatched)} key(s) that don't match any eligible "
            f"row in {TAXONOMY_CSV.relative_to(REPO_ROOT)}: "
            f"{sorted(unmatched)[:5]}"
        )
    if n_updated == 0:
        raise ValueError(
            f"sidecar at {sidecar} updated zero rows; either the sidecar is "
            f"empty or no rows are in eligible audit_status "
            f"{sorted(eligible_statuses)}."
        )

    with TAXONOMY_CSV.open("w", encoding="utf-8", newline="") as f:
        w = csv.DictWriter(f, fieldnames=fields, lineterminator="\n")
        w.writeheader()
        for r in rows:
            w.writerow(r)
    print(
        f"merged {n_updated} audit decisions into {TAXONOMY_CSV.relative_to(REPO_ROOT)}",
        file=sys.stderr,
    )
    return 0


def cmd_render(args: argparse.Namespace) -> int:
    fields, rows = _load_taxonomy_rows()
    audited = [r for r in rows if r["audit_status"] == "manual_confirmed"]
    out: list[str] = []
    out.append("# Failure taxonomy hand-audit — stratified sample (#194)")
    out.append("")
    out.append(
        f"*Audit pass over the {sum(1 for r in rows if r['audit_status'] in ('manual_confirmed', 'stratified_sample'))}"
        "-row stratified sample drawn by `scripts/build_failure_taxonomy.py` "
        "from PR #193's `results/metrics/failure_taxonomy_current.csv`. "
        f"Audited rows: {len(audited)}.*"
    )
    out.append("")
    out.append(
        "Auditor walked each row's judge log under `results/judge_logs/<run_name>"
        "/<scenario_id>_run<NN>_judge_log.json`, which embeds the agent's full "
        "trajectory + final answer. For each row the auditor decided: "
        "(a) `audit_decision` — `confirmed` (auto-label and trajectory symptoms "
        "agree), `relabel_suggested` (a concrete better label), or "
        "`evidence_thin` (judge symptom too weak/ambiguous to cite); (b) "
        "`berkeley_label` — `specification` / `inter_agent_orchestration` / "
        "`task_verification` per `docs/failure_taxonomy_evidence.md`; (c) "
        "`failure_stage` — earliest stage where the run becomes unrecoverable."
    )
    out.append("")

    # Methodology + Berkeley mapping rule come from the patterns sidecar so
    # the rendered doc is reproducible from committed inputs.
    patterns_json = (
        json.loads(_read_text(PATTERNS_JSON)) if _exists(PATTERNS_JSON) else {}
    )

    methodology = patterns_json.get("methodology") or {}
    if methodology:
        out.append(f"## {methodology.get('title', 'Methodology')}")
        out.append("")
        out.append(methodology.get("body", ""))
        out.append("")

    supersession = patterns_json.get("current_surface_supersession") or {}
    if supersession:
        out.append(
            f"## {supersession.get('title', 'Current taxonomy surface and supersession')}"
        )
        out.append("")
        out.append(supersession.get("body", ""))
        out.append("")

    rule = patterns_json.get("berkeley_mapping_rule") or {}
    if rule:
        out.append(f"## {rule.get('title', 'Berkeley-label mapping rule')}")
        out.append("")
        out.append(rule.get("body", ""))
        out.append("")

    stage_rule = patterns_json.get("failure_stage_rule") or {}
    if stage_rule:
        out.append(f"## {stage_rule.get('title', 'Failure-stage rule')}")
        out.append("")
        out.append(stage_rule.get("body", ""))
        out.append("")

    out.append("## Decision counts")
    out.append("")
    decisions = Counter(r["audit_decision"] for r in audited if r["audit_decision"])
    out.append("| audit_decision | count |")
    out.append("|---|---:|")
    for k in ("confirmed", "relabel_suggested", "evidence_thin"):
        out.append(f"| `{k}` | {decisions.get(k, 0)} |")
    out.append("")

    out.append("## Berkeley taxonomy distribution (audited rows)")
    out.append("")
    berkeley = Counter(r["berkeley_label"] for r in audited if r["berkeley_label"])
    out.append("| berkeley_label | count |")
    out.append("|---|---:|")
    for k, v in berkeley.most_common():
        out.append(f"| `{k}` | {v} |")
    out.append("")

    out.append("## Failure-stage distribution (audited rows)")
    out.append("")
    stages = Counter(r["failure_stage"] for r in audited if r["failure_stage"])
    out.append("| failure_stage | count |")
    out.append("|---|---:|")
    for k, v in stages.most_common():
        out.append(f"| `{k}` | {v} |")
    out.append("")

    if patterns_json:
        out.append("## Recurring failure patterns")
        out.append("")
        out.append(
            patterns_json.get(
                "lead",
                "These patterns recur across multiple audited rows and are the "
                "strongest paper-citable signals.",
            )
        )
        out.append("")
        for i, p in enumerate(patterns_json.get("patterns", []), 1):
            cells = ", ".join(p.get("cells", []))
            distinct = p.get("distinct_scenarios_count")
            distinct_ids = ", ".join(p.get("distinct_scenarios", []))
            scope = (
                f"{p['count']} rows across {distinct} distinct scenario{'s' if distinct != 1 else ''}"
                f" ({distinct_ids})"
                if distinct is not None
                else f"{p['count']} rows"
            )
            out.append(
                f"{i}. **{p['title']}** — {scope}. {p['description']} "
                f"Cells: {cells}. Mitigation candidate: {p['mitigation']}"
            )
        if "summary_note" in patterns_json:
            out.append("")
            out.append(patterns_json["summary_note"])
        out.append("")

        relabels = patterns_json.get("tie_break_relabels") or {}
        if relabels.get("rows"):
            has_v4 = any("v4" in r for r in relabels["rows"])
            if has_v4:
                out.append("## Tie-break relabels (v2 → v3 → v4)")
                out.append("")
                out.append(
                    "Six rows where v2's first-pass labels needed re-evaluation. "
                    "v3 changed the Berkeley labels per the latest-irreversible-mistake "
                    "tie-break rule (correct) but also applied that rule to "
                    "`failure_stage` (incorrect — stage uses earliest-unrecoverable "
                    "per `docs/failure_taxonomy_evidence.md:149-151`). v4 keeps "
                    "the v3 Berkeley relabels and re-evaluates each row's stage "
                    "independently."
                )
                out.append("")
                out.append(
                    "| cell | scenario | trial | v2 berkeley / stage | v3 berkeley / stage | v4 berkeley / stage | rationale |"
                )
                out.append("|---|---|---:|---|---|---|---|")
                for r in relabels["rows"]:
                    v2 = r["v2"]
                    v3 = r["v3"]
                    v4 = r.get("v4", v3)
                    rationale = r["rationale"].replace("|", "\\|")
                    out.append(
                        f"| {r['cell']} | {r['scenario_id']} | {r['trial_index']} | "
                        f"`{v2['berkeley']}` / `{v2['stage']}` | "
                        f"`{v3['berkeley']}` / `{v3['stage']}` | "
                        f"`{v4['berkeley']}` / `{v4['stage']}` | {rationale} |"
                    )
            else:
                out.append("## Tie-break relabels (v2 → v3)")
                out.append("")
                out.append(
                    "Six rows were relabeled in v3 to apply the team's tie-break rule "
                    "consistently (latest irreversible mistake). v2's labels reflected "
                    "the auditor's first-pass 'first irreversible mistake' reading; "
                    "v3 corrects this against `docs/failure_taxonomy_evidence.md:139-145`."
                )
                out.append("")
                out.append(
                    "| cell | scenario | trial | v2 berkeley / stage | v3 berkeley / stage | rationale |"
                )
                out.append("|---|---|---:|---|---|---|")
                for r in relabels["rows"]:
                    v2 = r["v2"]
                    v3 = r["v3"]
                    rationale = r["rationale"].replace("|", "\\|")
                    out.append(
                        f"| {r['cell']} | {r['scenario_id']} | {r['trial_index']} | "
                        f"`{v2['berkeley']}` / `{v2['stage']}` | "
                        f"`{v3['berkeley']}` / `{v3['stage']}` | {rationale} |"
                    )
            out.append("")

    out.append("## Per-row audit table")
    out.append("")
    out.append(
        "| cell | run | scenario | trial | auto_label | decision | berkeley | stage | note |"
    )
    out.append("|---|---|---|---:|---|---|---|---|---|")
    for r in audited:
        run_short = r["run_name"]
        if len(run_short) > 60:
            run_short = "..." + run_short[-58:]
        note = (r["audit_note"] or "").replace("|", "\\|")
        out.append(
            f"| {r['experiment_cell']} | `{run_short}` | {r['scenario_id']} | "
            f"{r['trial_index']} | `{r['auto_taxonomy_label'].removeprefix('low_')}` | "
            f"`{r['audit_decision']}` | `{r['berkeley_label']}` | "
            f"`{r['failure_stage']}` | {note} |"
        )
    out.append("")

    # Paper-cite candidates: confirmed rows with a clear concrete note.
    cited = [
        r
        for r in audited
        if r.get("audit_decision") == "confirmed"
        and (r.get("berkeley_label") or "")
        and (r.get("failure_stage") or "")
    ][: args.paper_cite_n]
    out.append(f"## Paper-cite candidates (top {len(cited)})")
    out.append("")
    out.append(
        "These rows have a confirmed auto-label, a concrete trajectory-grounded "
        "note, and a balanced cell/orchestration/symptom mix. Recommended "
        "examples for the paper's failure-analysis section."
    )
    out.append("")
    for i, r in enumerate(cited, 1):
        out.append(
            f"{i}. **Cell {r['experiment_cell']} / {r['orchestration_mode']} / "
            f"{r['scenario_id']} run{int(r['trial_index']):02d}** — "
            f"{r['auto_taxonomy_label']} -> `{r['berkeley_label']}` "
            f"({r['failure_stage']}). {r['audit_note']} "
            f"Source: `{r['trajectory_file']}`."
        )
    out.append("")

    text = "\n".join(out)
    if args.out:
        Path(args.out).write_text(text, encoding="utf-8")
        print(
            f"wrote {args.out}: {len(audited)} audited rows, {len(cited)} paper-cite candidates",
            file=sys.stderr,
        )
    else:
        sys.stdout.write(text)
    return 0


def main(argv: list[str] | None = None) -> int:
    p = argparse.ArgumentParser(description=__doc__)
    sp = p.add_subparsers(dest="cmd", required=True)

    pb = sp.add_parser("briefs", help="emit per-row briefs for the auditor")
    pb.add_argument("--out", type=str, default=None)
    pb.add_argument("--cell", type=str, default=None, help="filter by experiment_cell")
    pb.add_argument(
        "--label", type=str, default=None, help="filter by auto_taxonomy_label"
    )
    pb.add_argument(
        "--status",
        type=str,
        default=None,
        choices=("stratified_sample", "manual_confirmed"),
        help="filter by audit_status (default: include both)",
    )
    pb.set_defaults(fn=cmd_briefs)

    pa = sp.add_parser(
        "add-columns",
        help="merge a hand-edited audit-decisions sidecar TSV into the taxonomy CSV",
    )
    pa.add_argument(
        "--decisions",
        type=str,
        default=None,
        help=(
            "path to the audit-decisions TSV; defaults to "
            "data/audit/issue194_decisions.tsv (committed)"
        ),
    )
    pa.set_defaults(fn=cmd_add_columns)

    pr = sp.add_parser(
        "render", help="render markdown audit summary from the augmented CSV"
    )
    pr.add_argument("--out", type=str, default=None)
    pr.add_argument(
        "--paper-cite-n",
        type=int,
        default=10,
        help="max paper-cite candidates to surface",
    )
    pr.set_defaults(fn=cmd_render)

    args = p.parse_args(argv)
    return args.fn(args)


if __name__ == "__main__":
    raise SystemExit(main())