File size: 10,418 Bytes
c10636a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Summarize one or more evaluator-only Agent private-gold audits."""

from __future__ import annotations

import argparse
import json
import sys
from collections import Counter, defaultdict
from collections.abc import Mapping
from pathlib import Path
from typing import Any

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from eviscene.eval.private_gold_metrics import (
    agent_private_gold_category,
    agent_private_gold_category_counts,
)


SCHEMA_VERSION = "ifv-agent-test-evaluation-summary-v1"
VALID_VERDICTS = frozenset({"real", "fake"})


def _read_jsonl(path: Path) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    with path.open(encoding="utf-8") as handle:
        for line_number, line in enumerate(handle, start=1):
            if not line.strip():
                continue
            row = json.loads(line)
            if not isinstance(row, dict):
                raise ValueError(f"{path}:{line_number} must be an object")
            rows.append(row)
    return rows


def _write_json(path: Path, payload: Mapping[str, Any]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        json.dumps(dict(payload), ensure_ascii=False, indent=2) + "\n",
        encoding="utf-8",
    )


def _write_jsonl(path: Path, rows: list[Mapping[str, Any]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        "".join(json.dumps(dict(row), ensure_ascii=False) + "\n" for row in rows),
        encoding="utf-8",
    )


def _case_id(row: Mapping[str, Any]) -> str:
    value = str(
        row.get("unified_case_id")
        or row.get("archive_source_version_id")
        or row.get("case_id")
        or ""
    ).strip()
    if not value:
        raise ValueError("row lacks a stable case ID")
    return value


def _parse_input(value: str) -> tuple[str, Path]:
    name, separator, rendered_path = value.partition("=")
    if not separator or not name.strip() or not rendered_path.strip():
        raise argparse.ArgumentTypeError(
            "--input must be NAME=PATH, for example old100=/tmp/audit-results.jsonl"
        )
    return name.strip(), Path(rendered_path.strip()).expanduser().resolve()


def _metric_payload(rows: list[Mapping[str, Any]]) -> dict[str, Any]:
    completed = [
        row
        for row in rows
        if row.get("status") == "completed"
        and str(row.get("gold_verdict") or "").lower() in VALID_VERDICTS
        and str(row.get("candidate_verdict") or "").lower() in VALID_VERDICTS
    ]
    matrix = {
        expected: {predicted: 0 for predicted in sorted(VALID_VERDICTS)}
        for expected in sorted(VALID_VERDICTS)
    }
    for row in completed:
        expected = str(row["gold_verdict"]).lower()
        predicted = str(row["candidate_verdict"]).lower()
        matrix[expected][predicted] += 1

    total = sum(sum(predictions.values()) for predictions in matrix.values())
    correct = sum(matrix[label][label] for label in matrix)
    recall = {
        label: (
            matrix[label][label] / sum(matrix[label].values())
            if sum(matrix[label].values())
            else None
        )
        for label in matrix
    }
    available_recalls = [value for value in recall.values() if value is not None]
    return {
        "evaluated": total,
        "correct": correct,
        "accuracy": correct / total if total else None,
        "balanced_accuracy": (
            sum(available_recalls) / len(available_recalls)
            if available_recalls
            else None
        ),
        "recall_by_gold_verdict": recall,
        "confusion_gold_rows_candidate_columns": matrix,
    }


def _route_breakdown(rows: list[Mapping[str, Any]]) -> dict[str, dict[str, Any]]:
    groups: dict[str, list[Mapping[str, Any]]] = defaultdict(list)
    for row in rows:
        groups[str(row.get("construction_subroute") or "unknown")].append(row)
    output: dict[str, dict[str, Any]] = {}
    for route, route_rows in sorted(groups.items()):
        metrics = _metric_payload(route_rows)
        categories = agent_private_gold_category_counts(route_rows)
        output[route] = {
            "selected": len(route_rows),
            "completed": sum(row.get("status") == "completed" for row in route_rows),
            "engineering_or_judge_errors": sum(
                row.get("status") != "completed" for row in route_rows
            ),
            **metrics,
            "three_way_private_gold_categories": categories,
        }
    return output


def summarize_agent_test_evaluation(
    *,
    inputs: list[tuple[str, Path]],
    test_manifest: Path,
    output_dir: Path,
    expected_case_count: int | None = None,
) -> dict[str, Any]:
    """Join audit records with test metadata and calculate reported metrics."""

    test_manifest = test_manifest.expanduser().resolve()
    output_dir = output_dir.expanduser().resolve()
    manifest_rows = _read_jsonl(test_manifest)
    manifest_by_case: dict[str, dict[str, Any]] = {}
    for row in manifest_rows:
        case_id = _case_id(row)
        if case_id in manifest_by_case:
            raise ValueError(f"duplicate case ID in test manifest: {case_id}")
        manifest_by_case[case_id] = row

    merged: list[dict[str, Any]] = []
    seen: set[str] = set()
    source_counts: dict[str, int] = {}
    for source_name, path in inputs:
        if source_name in source_counts:
            raise ValueError(f"duplicate input name: {source_name}")
        source_rows = _read_jsonl(path)
        source_counts[source_name] = len(source_rows)
        for row in source_rows:
            case_id = _case_id(row)
            if case_id in seen:
                raise ValueError(f"case appears in multiple audit inputs: {case_id}")
            seen.add(case_id)
            manifest_row = manifest_by_case.get(case_id)
            if manifest_row is None:
                raise ValueError(f"audit case is absent from test manifest: {case_id}")
            combined = dict(row)
            combined["case_id"] = case_id
            combined["audit_source"] = source_name
            for field in (
                "construction_subroute",
                "production_class",
                "target_route",
                "capability_cell",
                "factual_status",
            ):
                combined[field] = manifest_row.get(field)
            combined["private_gold_category"] = agent_private_gold_category(combined)
            merged.append(combined)
    if expected_case_count is not None and len(merged) != expected_case_count:
        raise ValueError(
            f"expected {expected_case_count} audit rows, found {len(merged)}"
        )

    merged.sort(key=lambda row: row["case_id"])
    status_counts = Counter(str(row.get("status") or "unknown") for row in merged)
    reason_quality = Counter(
        str(row.get("reason_quality") or "unknown")
        for row in merged
        if row.get("status") == "completed"
    )
    fact_alignment = Counter(
        str(row.get("fact_alignment") or "unknown")
        for row in merged
        if row.get("status") == "completed"
    )
    failure_modes = Counter(
        str(mode)
        for row in merged
        if row.get("status") == "completed"
        for mode in (row.get("failure_modes") or [])
        if str(mode).strip()
    )
    wrong_rows = [
        row
        for row in merged
        if row.get("private_gold_category") == "wrong_verdict"
    ]
    insufficient_rows = [
        row
        for row in merged
        if row.get("private_gold_category") == "correct_verdict_insufficient_evidence"
    ]
    result = {
        "schema_version": SCHEMA_VERSION,
        "test_manifest": str(test_manifest),
        "inputs": {name: str(path) for name, path in inputs},
        "input_row_counts": source_counts,
        "selected": len(merged),
        "status_counts": dict(status_counts),
        "engineering_or_judge_errors": sum(
            row.get("status") != "completed" for row in merged
        ),
        "binary_metrics": _metric_payload(merged),
        "three_way_private_gold_categories": agent_private_gold_category_counts(
            merged
        ),
        "by_construction_subroute": _route_breakdown(merged),
        "failure_analysis": {
            "wrong_verdict_count": len(wrong_rows),
            "correct_but_insufficient_evidence_count": len(insufficient_rows),
            "wrong_verdict_by_subroute": dict(
                Counter(
                    str(row.get("construction_subroute") or "unknown")
                    for row in wrong_rows
                )
            ),
            "wrong_verdict_fact_alignment": dict(
                Counter(str(row.get("fact_alignment") or "unknown") for row in wrong_rows)
            ),
            "insufficient_evidence_reason_quality": dict(
                Counter(
                    str(row.get("reason_quality") or "unknown")
                    for row in insufficient_rows
                )
            ),
            "all_completed_reason_quality": dict(reason_quality),
            "all_completed_fact_alignment": dict(fact_alignment),
            "judge_failure_modes": dict(failure_modes),
        },
    }
    _write_jsonl(output_dir / "combined-audit-results.jsonl", merged)
    result["combined_audit_results"] = str(
        output_dir / "combined-audit-results.jsonl"
    )
    _write_json(output_dir / "summary.json", result)
    return result


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", action="append", required=True, type=_parse_input)
    parser.add_argument("--test-manifest", required=True)
    parser.add_argument("--output-dir", required=True)
    parser.add_argument("--expected-case-count", type=int)
    args = parser.parse_args()
    if args.expected_case_count is not None and args.expected_case_count < 1:
        parser.error("--expected-case-count must be positive")
    result = summarize_agent_test_evaluation(
        inputs=args.input,
        test_manifest=Path(args.test_manifest),
        output_dir=Path(args.output_dir),
        expected_case_count=args.expected_case_count,
    )
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


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