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
 
 
 
 
 
c10b0c0
 
 
 
 
 
 
 
12ea280
 
 
 
 
 
 
 
 
c10b0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12ea280
 
c10b0c0
 
 
 
 
 
 
 
 
 
 
 
 
12ea280
c10b0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12ea280
 
 
 
c10b0c0
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Build full-run and comparison shards for the canonical PhantomWiki split.

Run from the viewer repository root:

    python3 scripts/build_runs.py

The response files may contain multiple datasets. They are streamed line by
line, only ``dataset == "phantom_wiki"`` records are retained, and c1/c2
recovery rows overlay their original full-run responses.
"""

from __future__ import annotations

import argparse
import csv
import json
import shutil
from pathlib import Path
from typing import Any, Iterator


ROOT = Path(__file__).resolve().parents[1]
DATASET = "phantom_wiki"
SCOPE = "size5000_seed1"
STRING_LIMIT = 8192
EVENTS_LIMIT_BYTES = 256 * 1024

DEFAULT_EVAL = ROOT / "eval_size5000_seed1.json"
DEFAULT_SOURCES = {
    "c1": {
        "label": "c1 Closed-book",
        "response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_full/full_closedbook/named-outputs/response/response"
        ),
        "recovery_response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_recover/cb_len4x/named-outputs/response/response"
        ),
        "judge": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_judge/cb_phantom_len4x/named-outputs/judged/judged"
        ),
    },
    "c2": {
        "label": "c2 With-docs",
        "response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_full/full_openbook/named-outputs/response/response"
        ),
        "recovery_response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_recover/ob_len4x/named-outputs/response/response"
        ),
        "judge": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_judge/ob_phantom_len4x/named-outputs/judged/judged"
        ),
    },
    "c6": {
        "label": "c6 Agentic-DCI",
        "response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_full/full_dci/named-outputs/response/response"
        ),
        "judge": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_judge/dci_phantom/named-outputs/judged/judged"
        ),
    },
    "naive": {
        "label": "Naive-search",
        "response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_full/full_naive_phantom/named-outputs/response/response"
        ),
        "judge": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/"
            "herb_phantom_judge/naive_phantom/named-outputs/judged/judged"
        ),
    },
    "e2e": {
        "label": "E2E v3",
        "response": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/e2e_runs/"
            "new-datasets-full-20260711/phantom_wiki/named-outputs/"
            "predictions/predictions"
        ),
        "judge": Path(
            "/home/azureuser/projects/information-scaffolds/outputs/e2e_runs/"
            "new-datasets-full-20260711/judges/phantom_wiki/named-outputs/"
            "judged/judged"
        ),
    },
    "e2e_rawtext": {
        "label": "E2E v3 + rawtext",
        "response": Path(
            "/tmp/viewer-overlay-phantom/predictions/named-outputs/"
            "predictions/predictions"
        ),
        "judge": Path(
            "/tmp/viewer-overlay-phantom/evaluated/named-outputs/"
            "canonical_evaluated/evaluated"
        ),
        "judge_format": "f1_csv",
        "score_label": "Mean answer F1",
    },
}


def stream_jsonl(path: Path) -> Iterator[dict[str, Any]]:
    with path.open(encoding="utf-8") as handle:
        for line_number, line in enumerate(handle, 1):
            if not line.strip():
                continue
            try:
                yield json.loads(line)
            except json.JSONDecodeError as exc:
                raise ValueError(f"{path}:{line_number}: {exc}") from exc


def load_dataset_rows(path: Path) -> dict[str, dict[str, Any]]:
    rows: dict[str, dict[str, Any]] = {}
    for row in stream_jsonl(path):
        if row.get("dataset") != DATASET:
            continue
        qid = str(row.get("qid") or "")
        if not qid:
            raise ValueError(f"{path}: PhantomWiki row has no qid")
        if qid in rows:
            raise ValueError(f"{path}: duplicate PhantomWiki qid {qid}")
        rows[qid] = row
    return rows


def load_f1_rows(path: Path) -> dict[str, dict[str, Any]]:
    rows: dict[str, dict[str, Any]] = {}
    with path.open(encoding="utf-8", newline="") as handle:
        for row in csv.DictReader(handle):
            qid = str(row.get("qid") or "")
            if not qid:
                raise ValueError(f"{path}: F1 row has no qid")
            if qid in rows:
                raise ValueError(f"{path}: duplicate F1 qid {qid}")
            rows[qid] = {
                "qid": qid,
                "precision": float(row["precision"]),
                "recall": float(row["recall"]),
                "f1": float(row["f1"]),
                "n_pred": int(row["n_pred"]),
                "n_gold": int(row["n_gold"]),
                "n_hit": int(row["n_hit"]),
            }
    return rows


def cap_string(value: str, limit: int = STRING_LIMIT) -> str:
    if len(value) <= limit:
        return value
    marker = f"\n… [truncated from {len(value)} chars]"
    return value[: max(0, limit - len(marker))] + marker


def cap_value(value: Any) -> Any:
    if isinstance(value, str):
        return cap_string(value)
    if isinstance(value, list):
        return [cap_value(item) for item in value]
    if isinstance(value, tuple):
        return [cap_value(item) for item in value]
    if isinstance(value, dict):
        return {cap_string(str(key)): cap_value(item) for key, item in value.items()}
    return value


def compact_events(events: Any) -> list[dict[str, Any]]:
    if not isinstance(events, list):
        return []
    compacted: list[dict[str, Any]] = []
    used = 2
    original_bytes = len(
        json.dumps(events, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
    )
    for index, raw in enumerate(events):
        if not isinstance(raw, dict):
            event = {"type": "event", "content": cap_value(raw)}
        else:
            event = {
                key: cap_value(raw[key])
                for key in ("type", "name", "input", "content")
                if raw.get(key) is not None
            }
        encoded = json.dumps(event, ensure_ascii=False, separators=(",", ":")).encode(
            "utf-8"
        )
        if used + len(encoded) + 1 > EVENTS_LIMIT_BYTES:
            compacted.append(
                {
                    "type": "truncated",
                    "content": (
                        f"{len(events) - index} events omitted; original events "
                        f"were {original_bytes} bytes and the compact limit is "
                        f"{EVENTS_LIMIT_BYTES} bytes."
                    ),
                }
            )
            break
        compacted.append(event)
        used += len(encoded) + 1
    return compacted


def nonempty_text(value: Any) -> str:
    return value.strip() if isinstance(value, str) else ""


def failure_reason(response: dict[str, Any] | None, answered: bool) -> str | None:
    if answered:
        return None
    if not response:
        return "missing_response"
    for key in ("stop_reason", "finish_reason"):
        if response.get(key):
            return str(response[key])
    for event in reversed(response.get("events") or []):
        if isinstance(event, dict) and event.get("type") == "error":
            return nonempty_text(event.get("content")) or "error"
    attempts = response.get("attempt_log") or []
    for attempt in reversed(attempts):
        if not isinstance(attempt, dict):
            continue
        if attempt.get("exc"):
            exc = str(attempt["exc"]).split(":", 1)[0]
            return f"exc:{exc}"
        if attempt.get("outcome") and attempt.get("outcome") != "success":
            return str(attempt["outcome"])
    return "unanswered"


def normalized_tokens(response: dict[str, Any] | None) -> dict[str, Any]:
    if not response:
        return {}
    tokens = response.get("tokens")
    if not isinstance(tokens, dict):
        tokens = response.get("usage")
    return cap_value(tokens) if isinstance(tokens, dict) else {}


def normalize_record(
    gold: dict[str, Any],
    response: dict[str, Any] | None,
    judge: dict[str, Any] | None,
) -> dict[str, Any]:
    prediction = nonempty_text((response or {}).get("answer"))
    answered = bool(prediction)
    is_f1 = isinstance((judge or {}).get("f1"), float)
    parsed = (judge or {}).get("parsed")
    parsed = parsed if isinstance(parsed, dict) else {}
    score = float(judge["f1"]) if is_f1 else None
    correct = bool(
        answered
        and (
            (score == 1.0 if score is not None else parsed.get("correct") is True)
        )
    )
    tool_counts = (response or {}).get("tool_call_counts")
    tool_counts = tool_counts if isinstance(tool_counts, dict) else {}
    metadata = gold.get("meta") if isinstance(gold.get("meta"), dict) else {}

    return cap_value(
        {
            "qid": gold["id"],
            "question": gold.get("question") or "",
            "gold": gold.get("answer") or [],
            "metadata": {
                "difficulty": metadata.get("Difficulty"),
                "type": metadata.get("Type"),
                "template": gold.get("template") or "",
                "prolog": gold.get("prolog") or [],
                "prolog_answer": gold.get("prolog_answer") or "",
                "supporting_titles": gold.get("supporting_titles") or [],
            },
            "prediction": prediction,
            "extracted_judge_answer": parsed.get("extracted_final_answer"),
            "answered": answered,
            "correct": correct,
            "score": score,
            "score_kind": "answer_f1" if score is not None else "binary",
            "score_details": (
                {
                    key: judge[key]
                    for key in ("precision", "recall", "f1", "n_pred", "n_gold", "n_hit")
                }
                if score is not None
                else None
            ),
            "judge": {
                "text": (
                    (
                        f"precision={judge['precision']:.4f}, "
                        f"recall={judge['recall']:.4f}, f1={judge['f1']:.4f}"
                    )
                    if score is not None
                    else (judge or {}).get("judge_text")
                ),
                "confidence": parsed.get("confidence"),
                "parse_error": parsed.get("parse_error"),
                "model": (judge or {}).get("judge_model"),
                "finish_reason": (judge or {}).get("finish_reason"),
            },
            "stop_reason": (response or {}).get("stop_reason"),
            "finish_reason": (response or {}).get("finish_reason"),
            "finish_reasons": (response or {}).get("finish_reasons") or [],
            "failure_reason": failure_reason(response, answered),
            "tokens": normalized_tokens(response),
            "turns": (response or {}).get("turns"),
            "tool_call_counts": tool_counts,
            "tool_calls": sum(
                value for value in tool_counts.values() if isinstance(value, int)
            ),
            "model": (response or {}).get("model"),
            "mode": (response or {}).get("mode"),
            "events": compact_events((response or {}).get("events")),
        }
    )


def status_for(record: dict[str, Any]) -> str:
    if not record["answered"]:
        return "missing"
    if isinstance(record.get("score"), (int, float)) and 0 < record["score"] < 1:
        return "partial"
    return "correct" if record["correct"] else "incorrect"


def run_summary(record: dict[str, Any]) -> dict[str, Any]:
    return {
        "qid": record["qid"],
        "question": record["question"],
        "gold": record["gold"],
        "answered": record["answered"],
        "correct": record["correct"],
        "score": record.get("score"),
        "score_kind": record.get("score_kind"),
        "status": status_for(record),
        "failure_reason": record["failure_reason"],
    }


def compare_projection(record: dict[str, Any], slot: str) -> dict[str, Any]:
    return {
        key: record[key]
        for key in (
            "prediction",
            "extracted_judge_answer",
            "answered",
            "correct",
            "score",
            "score_kind",
            "score_details",
            "judge",
            "stop_reason",
            "finish_reason",
            "finish_reasons",
            "failure_reason",
            "tokens",
            "turns",
            "tool_call_counts",
            "tool_calls",
            "model",
            "mode",
        )
    } | {"events_path": f"runs/{slot}/records/{record['qid']}.json"}


def write_json(path: Path, value: Any, *, indent: int | None = None) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8") as handle:
        json.dump(value, handle, ensure_ascii=False, indent=indent)


def clear_outputs(output_root: Path, compare_root: Path) -> None:
    if output_root.exists():
        shutil.rmtree(output_root)
    if compare_root.exists():
        shutil.rmtree(compare_root)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--eval-file", type=Path, default=DEFAULT_EVAL)
    parser.add_argument("--output-root", type=Path, default=ROOT / "runs")
    parser.add_argument("--compare-root", type=Path, default=ROOT / "compare")
    for slot, source in DEFAULT_SOURCES.items():
        parser.add_argument(
            f"--{slot}-response", type=Path, default=source["response"]
        )
        if source.get("recovery_response"):
            parser.add_argument(
                f"--{slot}-recovery-response",
                type=Path,
                default=source["recovery_response"],
            )
        parser.add_argument(f"--{slot}-judge", type=Path, default=source["judge"])
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    gold_rows = json.loads(args.eval_file.read_text(encoding="utf-8"))
    if len(gold_rows) != 500:
        raise ValueError(f"{args.eval_file}: expected 500 eval rows, got {len(gold_rows)}")
    qids = [str(row["id"]) for row in gold_rows]
    if len(qids) != len(set(qids)):
        raise ValueError(f"{args.eval_file}: duplicate eval qids")
    qid_set = set(qids)

    clear_outputs(args.output_root, args.compare_root)
    records_by_slot: dict[str, dict[str, dict[str, Any]]] = {}
    manifest_slots = []

    for slot, source in DEFAULT_SOURCES.items():
        response_path = getattr(args, f"{slot}_response")
        recovery_response_path = getattr(
            args, f"{slot}_recovery_response", None
        )
        judge_path = getattr(args, f"{slot}_judge")
        responses = load_dataset_rows(response_path)
        if recovery_response_path:
            responses.update(load_dataset_rows(recovery_response_path))
        judges = (
            load_f1_rows(judge_path)
            if source.get("judge_format") == "f1_csv"
            else load_dataset_rows(judge_path)
        )
        extra_response_qids = set(responses) - qid_set
        if extra_response_qids:
            raise ValueError(
                f"{slot}: response file contains "
                f"{len(extra_response_qids)} qids outside eval"
            )
        if not set(judges).issubset(qid_set):
            raise ValueError(f"{slot}: judge file contains qids outside eval")

        slot_records: dict[str, dict[str, Any]] = {}
        summaries = []
        records_dir = args.output_root / slot / "records"
        for gold in gold_rows:
            qid = str(gold["id"])
            record = normalize_record(gold, responses.get(qid), judges.get(qid))
            slot_records[qid] = record
            summaries.append(run_summary(record))
            write_json(records_dir / f"{qid}.json", record)

        answered = sum(record["answered"] for record in slot_records.values())
        correct = sum(record["correct"] for record in slot_records.values())
        canonical_score = sum(
            record["score"]
            if isinstance(record.get("score"), (int, float))
            else float(record["correct"])
            for record in slot_records.values()
        ) / len(qids)
        index = {
            "slot": slot,
            "label": source["label"],
            "scope": SCOPE,
            "total": len(qids),
            "answered": answered,
            "correct": correct,
            "coverage": answered / len(qids),
            "score": canonical_score,
            "score_label": source.get("score_label", "Canonical score"),
            "score_detail": (
                f"{correct} perfect · "
                f"{sum((record.get('score') or 0) > 0 for record in slot_records.values())} "
                "with answer overlap"
                if source.get("judge_format") == "f1_csv"
                else f"{correct} correct"
            ),
            "records": summaries,
        }
        write_json(args.output_root / slot / "index.json", index)
        records_by_slot[slot] = slot_records
        manifest_slots.append(
            {
                key: index[key]
                for key in (
                    "slot",
                    "label",
                    "scope",
                    "total",
                    "answered",
                    "correct",
                    "coverage",
                    "score",
                    "score_label",
                    "score_detail",
                )
            }
            | {
                "index": f"runs/{slot}/index.json",
                "records": f"runs/{slot}/records",
                "response_source": str(response_path),
                "recovery_response_source": (
                    str(recovery_response_path) if recovery_response_path else None
                ),
                "judge_source": str(judge_path),
            }
        )
        print(
            f"{slot}: score={canonical_score:.2%}, perfect/correct={correct}/{len(qids)}, "
            f"answered={answered}/{len(qids)} ({answered / len(qids):.2%})"
        )

    compare_summaries = []
    for gold in gold_rows:
        qid = str(gold["id"])
        run_records = {
            slot: records_by_slot[slot][qid] for slot in DEFAULT_SOURCES
        }
        correct_flags = [record["correct"] for record in run_records.values()]
        answered_flags = [record["answered"] for record in run_records.values()]
        summary = {
            "qid": qid,
            "question": gold.get("question") or "",
            "correct": {
                slot: record["correct"] for slot, record in run_records.items()
            },
            "answered": {
                slot: record["answered"] for slot, record in run_records.items()
            },
            "correct_count": sum(correct_flags),
            "answered_count": sum(answered_flags),
            "disagreement": len(set(correct_flags)) > 1,
            "any_missing": not all(answered_flags),
            "only_e2e_correct": (
                run_records["e2e"]["correct"]
                and all(
                    not record["correct"]
                    for slot, record in run_records.items()
                    if slot != "e2e"
                )
            ),
        }
        compare_summaries.append(summary)
        compare_record = {
            "qid": qid,
            "question": gold.get("question") or "",
            "gold": gold.get("answer") or [],
            "metadata": run_records["c1"]["metadata"],
            "runs": {
                slot: compare_projection(record, slot)
                for slot, record in run_records.items()
            },
        }
        write_json(args.compare_root / "records" / f"{qid}.json", compare_record)

    write_json(
        args.compare_root / "index.json",
        {
            "scope": SCOPE,
            "total": len(qids),
            "slots": list(DEFAULT_SOURCES),
            "records": compare_summaries,
        },
    )
    write_json(
        args.output_root / "manifest.json",
        {
            "dataset": DATASET,
            "scope": SCOPE,
            "scope_label": "depth_20_size_5000_seed_1",
            "eval_source": str(args.eval_file),
            "total": len(qids),
            "slots": manifest_slots,
            "compare_index": "compare/index.json",
            "scoring": (
                "Run-specific canonical score over all 500 eval qids; "
                "missing or unanswered is zero"
            ),
        },
        indent=2,
    )
    print(f"compare: {len(compare_summaries)} records")


if __name__ == "__main__":
    main()