File size: 30,073 Bytes
6c5f29f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
from __future__ import annotations

import argparse
import json
import re
import string
from collections import Counter, defaultdict
from pathlib import Path
from typing import Iterable

from longmemeval_reader_eval import (
    FOCUS_TYPES,
    METHOD_LABELS,
    ContextEntry,
    entries_from_full_raw,
    is_insufficient_answer,
    load_examples,
    reconstruct_context,
    token_f1,
)


DEFAULT_RUN_DIR = Path("llm_memory_validation/longmemeval_reader_api_gpt55_answer_supported_focus_full")
DEFAULT_OUT_DIR = Path("llm_memory_validation/scoring_audit_gpt55")
DEFAULT_DATASET = Path("llm_memory_validation/cache/longmemeval_s_cleaned.json")
DEFAULT_RETRIEVAL_ROWS = Path("llm_memory_validation/competitor_run_v2/retrieval_rows.json")

ORACLE_METHOD = "dense_budgeted_bsc"
FULL_RAW_METHOD = "dense_rag_e5"
HIGH_F1_THRESHOLD = 0.5

ARTICLES = {"a", "an", "the"}
MONTHS = {
    "january": "01",
    "jan": "01",
    "february": "02",
    "feb": "02",
    "march": "03",
    "mar": "03",
    "april": "04",
    "apr": "04",
    "may": "05",
    "june": "06",
    "jun": "06",
    "july": "07",
    "jul": "07",
    "august": "08",
    "aug": "08",
    "september": "09",
    "sep": "09",
    "sept": "09",
    "october": "10",
    "oct": "10",
    "november": "11",
    "nov": "11",
    "december": "12",
    "dec": "12",
}
NUMBER_WORDS = {
    "zero": "0",
    "one": "1",
    "two": "2",
    "three": "3",
    "four": "4",
    "five": "5",
    "six": "6",
    "seven": "7",
    "eight": "8",
    "nine": "9",
    "ten": "10",
    "eleven": "11",
    "twelve": "12",
    "thirteen": "13",
    "fourteen": "14",
    "fifteen": "15",
    "sixteen": "16",
    "seventeen": "17",
    "eighteen": "18",
    "nineteen": "19",
    "twenty": "20",
}


def read_jsonl(path: Path) -> list[dict]:
    rows: list[dict] = []
    with path.open(encoding="utf-8") as handle:
        for line in handle:
            stripped = line.strip()
            if stripped:
                rows.append(json.loads(stripped))
    return rows


def load_reader_outputs(run_dir: Path) -> list[dict]:
    jsonl_path = run_dir / "reader_outputs.jsonl"
    if jsonl_path.exists():
        return read_jsonl(jsonl_path)
    predictions_path = run_dir / "predictions.json"
    artifacts = json.loads(predictions_path.read_text(encoding="utf-8"))
    rows: list[dict] = []
    for method_rows in artifacts.values():
        rows.extend(method_rows)
    return rows


def strip_parentheticals(text: str) -> str:
    return re.sub(r"\([^)]*\)", " ", text)


def normalize_aliases(text: str) -> str:
    replacements = [
        (r"\bU\.?\s*S\.?\s*A\.?\b", " United States "),
        (r"\bU\.?\s*S\.?\b", " United States "),
        (r"\bUnited States of America\b", " United States "),
        (r"\bU\.?\s*K\.?\b", " United Kingdom "),
        (r"\bNew York City\b", " NYC "),
    ]
    result = text
    for pattern, repl in replacements:
        result = re.sub(pattern, repl, result, flags=re.IGNORECASE)
    return result


def normalize_date_mentions(text: str) -> str:
    text = re.sub(r"\b(\d{1,2})(st|nd|rd|th)\b", r"\1", text, flags=re.IGNORECASE)

    def month_day_year(match: re.Match[str]) -> str:
        month = MONTHS[match.group(1).lower()]
        day = int(match.group(2))
        year = match.group(3)
        if year:
            return f" {year}-{month}-{day:02d} "
        return f" {month}-{day:02d} "

    text = re.sub(
        r"\b("
        + "|".join(MONTHS)
        + r")\s+(\d{1,2})(?:,\s*|\s+)?(\d{4})?\b",
        month_day_year,
        text,
        flags=re.IGNORECASE,
    )

    def slash_date(match: re.Match[str]) -> str:
        first = int(match.group(1))
        second = int(match.group(2))
        year = match.group(3)
        if year:
            return f" {year}-{first:02d}-{second:02d} "
        return f" {first:02d}-{second:02d} "

    text = re.sub(r"\b(\d{1,2})[/-](\d{1,2})(?:[/-](\d{2,4}))?\b", slash_date, text)
    return text


def normalize_number_words(text: str) -> str:
    def repl(match: re.Match[str]) -> str:
        return NUMBER_WORDS[match.group(0).lower()]

    return re.sub(r"\b(" + "|".join(NUMBER_WORDS) + r")\b", repl, text, flags=re.IGNORECASE)


def normalized_answer(text: str) -> str:
    text = normalize_aliases(str(text))
    text = normalize_date_mentions(text)
    text = normalize_number_words(text)
    text = text.lower()
    text = text.translate(str.maketrans("", "", string.punctuation))
    tokens = [token for token in text.split() if token not in ARTICLES]
    return " ".join(tokens)


def gold_variants(gold: str) -> list[str]:
    raw = str(gold).strip()
    variants = [raw]
    no_parens = strip_parentheticals(raw).strip()
    if no_parens and no_parens != raw:
        variants.append(no_parens)

    acceptable_split = re.split(
        r"\.\s*|;\s*|\bis also acceptable\b|\bare also acceptable\b|\balso acceptable\b",
        no_parens,
        flags=re.IGNORECASE,
    )
    for part in acceptable_split:
        part = re.sub(r"\b(including|also)\b.*$", "", part.strip(), flags=re.IGNORECASE).strip()
        if part:
            variants.append(part)

    for sep in [" / ", " or "]:
        if sep in no_parens.lower():
            pattern = re.compile(re.escape(sep), flags=re.IGNORECASE)
            variants.extend(part.strip() for part in pattern.split(no_parens) if part.strip())

    seen: set[str] = set()
    unique: list[str] = []
    for variant in variants:
        normalized = normalized_answer(variant)
        if normalized and normalized not in seen:
            seen.add(normalized)
            unique.append(variant)
    return unique


def normalized_exact_match(prediction: str, gold: str) -> float:
    pred_norm = normalized_answer(prediction)
    if not pred_norm:
        return 0.0
    return float(any(pred_norm == normalized_answer(variant) for variant in gold_variants(gold)))


def infer_answer_type(question: str, gold: str) -> str:
    question_l = str(question).lower()
    gold_l = str(gold).lower()
    combined = f"{question_l} {gold_l}"
    if is_insufficient_answer(gold) or gold_l in {"unknown", "not enough information", "insufficient evidence"}:
        return "unknown/insufficient"
    if re.search(
        r"\b(when|what date|what day|what time|how long|days?|weeks?|months?|years?|"
        r"monday|tuesday|wednesday|thursday|friday|saturday|sunday|"
        + "|".join(MONTHS)
        + r"|\d{1,2}:\d{2}|\d{4})\b",
        combined,
    ):
        return "date/time"
    if re.search(r"\b(where|location|venue|city|country|state|address|airport|hotel|restaurant|museum|park)\b", combined):
        return "location"
    if re.search(r"\b(who|whose|person|name|friend|doctor|teacher|manager|partner|colleague|author|artist|band)\b", combined):
        return "person/name"
    if re.search(r"\b(prefer|preference|favorite|favourite|like|love|dislike|allerg|diet|order|want|usually)\b", combined):
        return "preference"
    if re.search(r"\b(event|happened|concert|trip|meeting|appointment|flight|visit|party|wedding|first|last|before|after|sequence|order)\b", combined):
        return "event"
    if normalized_answer(gold):
        return "free-form fact"
    return "unknown/insufficient"


def mean(rows: list[dict], field: str) -> float:
    if not rows:
        return 0.0
    return sum(float(row.get(field, 0.0)) for row in rows) / len(rows)


def summarize_rows(rows: list[dict]) -> dict:
    return {
        "n": len(rows),
        "raw_em": mean(rows, "raw_em"),
        "normalized_em": mean(rows, "normalized_em"),
        "token_f1": mean(rows, "token_f1"),
        "evidence_use": mean(rows, "evidence_use"),
        "insufficient_evidence_rate": mean(rows, "abstained_float"),
        "unsupported_answer_rate": mean(rows, "unsupported_answer"),
        "parse_failure_rate": mean(rows, "parse_failure_float"),
        "gold_evidence_retrieved": mean(rows, "gold_evidence_retrieved_float"),
    }


def enrich_rows(rows: list[dict], examples_by_id: dict[str, dict]) -> list[dict]:
    enriched: list[dict] = []
    for row in rows:
        example = examples_by_id.get(row.get("question_id"), {})
        gold_ids = set(row.get("gold_session_ids", []))
        context_ids = set(row.get("context_session_ids", []))
        gold_retrieved = bool(gold_ids & context_ids)
        question = example.get("question", "")
        gold = row.get("gold_answer", example.get("answer", ""))
        prediction = row.get("prediction", "")
        raw_em = float(row.get("exact_match", 0.0))
        norm_em = normalized_exact_match(prediction, gold)
        enriched.append(
            {
                **row,
                "question": question,
                "gold": gold,
                "answer": prediction,
                "raw_em": raw_em,
                "normalized_em": norm_em,
                "token_f1": float(row.get("token_f1", token_f1(prediction, gold))),
                "abstained_float": float(bool(row.get("abstained"))),
                "parse_failure_float": float(bool(row.get("parse_failure"))),
                "gold_evidence_retrieved": gold_retrieved,
                "gold_evidence_retrieved_float": float(gold_retrieved),
                "gold_recall_in_context": len(gold_ids & context_ids) / max(len(gold_ids), 1),
                "answer_type": infer_answer_type(question, gold),
            }
        )
    return enriched


def by_method(rows: Iterable[dict]) -> dict[str, list[dict]]:
    grouped: dict[str, list[dict]] = defaultdict(list)
    for row in rows:
        grouped[row["method"]].append(row)
    return dict(grouped)


def answer_type_summary(rows: list[dict]) -> dict:
    result: dict[str, dict] = {}
    for method, method_rows in sorted(by_method(rows).items()):
        type_rows: dict[str, list[dict]] = defaultdict(list)
        for row in method_rows:
            type_rows[row["answer_type"]].append(row)
        result[method] = {
            "method_label": method_rows[0].get("method_label", METHOD_LABELS.get(method, method)),
            "answer_types": {
                answer_type: summarize_rows(type_group)
                for answer_type, type_group in sorted(type_rows.items())
            },
        }
    return result


def method_summary(rows: list[dict], focus_types: set[str]) -> dict:
    summary: dict[str, dict] = {}
    for method, method_rows in sorted(by_method(rows).items()):
        focus_rows = [row for row in method_rows if row.get("question_type") in focus_types]
        summary[method] = {
            "method_label": method_rows[0].get("method_label", METHOD_LABELS.get(method, method)),
            "overall": summarize_rows(method_rows),
            "focus": summarize_rows(focus_rows),
        }
    return summary


def retrieval_lookup(retrieval_rows: dict[str, list[dict]]) -> dict[str, dict[str, dict]]:
    return {
        method: {row["question_id"]: row for row in method_rows}
        for method, method_rows in retrieval_rows.items()
    }


def raw_context_from_row(row: dict, examples_by_id: dict[str, dict]) -> list[ContextEntry]:
    example = examples_by_id.get(row.get("question_id"), {})
    full_raw = entries_from_full_raw(example) if example else {}
    return [full_raw[session_id] for session_id in row.get("context_session_ids", []) if session_id in full_raw]


def context_for_row(

    row: dict,

    contexts: dict[tuple[str, str], list[ContextEntry]],

    examples_by_id: dict[str, dict],

    retrieval_by_method: dict[str, dict[str, dict]],

    budget_frac: float,

    max_context_words: int,

) -> list[ContextEntry]:
    key = (row["method"], row["question_id"])
    if key in contexts:
        return contexts[key]
    example = examples_by_id.get(row["question_id"])
    retrieval_row = retrieval_by_method.get(row["method"], {}).get(row["question_id"])
    if example is not None and retrieval_row is not None:
        context, _fallbacks = reconstruct_context(
            example,
            retrieval_row,
            row["method"],
            budget_frac,
            max_context_words,
        )
    else:
        context = raw_context_from_row(row, examples_by_id)
    contexts[key] = context
    return context


def memories_for_row(

    row: dict,

    contexts: dict[tuple[str, str], list[ContextEntry]],

    examples_by_id: dict[str, dict],

    retrieval_by_method: dict[str, dict[str, dict]],

    budget_frac: float,

    max_context_words: int,

    max_chars: int,

) -> list[dict]:
    context = context_for_row(
        row,
        contexts,
        examples_by_id,
        retrieval_by_method,
        budget_frac,
        max_context_words,
    )
    gold_ids = set(row.get("gold_session_ids", []))
    used_ids = set(row.get("used_memory_ids", []))
    memories = []
    for entry in context:
        memories.append(
            {
                "memory_id": entry.session_id,
                "action": entry.action,
                "source": entry.source,
                "is_gold_evidence": entry.session_id in gold_ids,
                "used_by_reader": entry.session_id in used_ids,
                "text": entry.text[:max_chars],
            }
        )
    return memories


def sample_payload(

    row: dict,

    audit_category: str,

    contexts: dict[tuple[str, str], list[ContextEntry]],

    examples_by_id: dict[str, dict],

    retrieval_by_method: dict[str, dict[str, dict]],

    budget_frac: float,

    max_context_words: int,

    max_memory_chars: int,

    paired_row: dict | None = None,

) -> dict:
    payload = {
        "audit_category": audit_category,
        "question_id": row.get("question_id"),
        "question_type": row.get("question_type"),
        "answer_type": row.get("answer_type"),
        "question": row.get("question", ""),
        "gold": row.get("gold", row.get("gold_answer", "")),
        "method": row.get("method"),
        "method_label": row.get("method_label", METHOD_LABELS.get(row.get("method", ""), row.get("method", ""))),
        "answer": row.get("answer", row.get("prediction", "")),
        "retrieved_memories": memories_for_row(
            row,
            contexts,
            examples_by_id,
            retrieval_by_method,
            budget_frac,
            max_context_words,
            max_memory_chars,
        ),
        "used_memory_ids": row.get("used_memory_ids", []),
        "raw_em": row.get("raw_em", 0.0),
        "normalized_em": row.get("normalized_em", 0.0),
        "f1": row.get("token_f1", 0.0),
        "gold_evidence_retrieved": bool(row.get("gold_evidence_retrieved")),
        "gold_recall_in_context": row.get("gold_recall_in_context", 0.0),
        "evidence_use": row.get("evidence_use", 0.0),
        "abstained": bool(row.get("abstained")),
        "unsupported_answer": row.get("unsupported_answer", 0.0),
        "gold_session_ids": row.get("gold_session_ids", []),
        "context_session_ids": row.get("context_session_ids", []),
    }
    if paired_row is not None:
        payload["paired_full_raw"] = {
            "method": paired_row.get("method"),
            "method_label": paired_row.get("method_label", METHOD_LABELS.get(paired_row.get("method", ""), "")),
            "answer": paired_row.get("answer", paired_row.get("prediction", "")),
            "raw_em": paired_row.get("raw_em", 0.0),
            "normalized_em": paired_row.get("normalized_em", 0.0),
            "f1": paired_row.get("token_f1", 0.0),
            "gold_evidence_retrieved": bool(paired_row.get("gold_evidence_retrieved")),
            "evidence_use": paired_row.get("evidence_use", 0.0),
            "abstained": bool(paired_row.get("abstained")),
            "used_memory_ids": paired_row.get("used_memory_ids", []),
            "context_session_ids": paired_row.get("context_session_ids", []),
        }
    return payload


def select_top(rows: list[dict], limit: int, used_keys: set[tuple[str, str]], key_fn) -> list[dict]:
    selected: list[dict] = []
    for row in sorted(rows, key=key_fn):
        row_key = (row["method"], row["question_id"])
        if row_key in used_keys:
            continue
        selected.append(row)
        used_keys.add(row_key)
        if len(selected) >= limit:
            break
    return selected


def build_balanced_sample(

    rows: list[dict],

    contexts: dict[tuple[str, str], list[ContextEntry]],

    examples_by_id: dict[str, dict],

    retrieval_by_method: dict[str, dict[str, dict]],

    budget_frac: float,

    max_context_words: int,

    max_memory_chars: int,

) -> tuple[list[dict], dict]:
    rows_by_method = by_method(rows)
    oracle_rows = rows_by_method.get(ORACLE_METHOD, [])
    full_rows = rows_by_method.get(FULL_RAW_METHOD, [])
    full_by_qid = {row["question_id"]: row for row in full_rows}
    used_keys: set[tuple[str, str]] = set()

    sample_rows: list[dict] = []
    category_counts: dict[str, int] = {}

    category = "oraclemem_abstained_despite_support"
    selected = select_top(
        [
            row
            for row in oracle_rows
            if row.get("gold_evidence_retrieved") and row.get("abstained")
        ],
        20,
        used_keys,
        key_fn=lambda row: (row.get("question_type", ""), row.get("question_id", "")),
    )
    category_counts[category] = len(selected)
    sample_rows.extend(
        sample_payload(
            row,
            category,
            contexts,
            examples_by_id,
            retrieval_by_method,
            budget_frac,
            max_context_words,
            max_memory_chars,
        )
        for row in selected
    )

    category = "oraclemem_high_f1_em0"
    selected = select_top(
        [
            row
            for row in oracle_rows
            if row.get("raw_em", 0.0) == 0.0
            and row.get("token_f1", 0.0) >= HIGH_F1_THRESHOLD
            and not row.get("abstained")
        ],
        10,
        used_keys,
        key_fn=lambda row: (-row.get("token_f1", 0.0), row.get("question_id", "")),
    )
    category_counts[category] = len(selected)
    sample_rows.extend(
        sample_payload(
            row,
            category,
            contexts,
            examples_by_id,
            retrieval_by_method,
            budget_frac,
            max_context_words,
            max_memory_chars,
        )
        for row in selected
    )

    category = "full_raw_high_f1_em0"
    selected = select_top(
        [
            row
            for row in full_rows
            if row.get("raw_em", 0.0) == 0.0
            and row.get("token_f1", 0.0) >= HIGH_F1_THRESHOLD
            and not row.get("abstained")
        ],
        10,
        used_keys,
        key_fn=lambda row: (-row.get("token_f1", 0.0), row.get("question_id", "")),
    )
    category_counts[category] = len(selected)
    sample_rows.extend(
        sample_payload(
            row,
            category,
            contexts,
            examples_by_id,
            retrieval_by_method,
            budget_frac,
            max_context_words,
            max_memory_chars,
        )
        for row in selected
    )

    category = "oraclemem_full_raw_disagreement"
    disagreement_rows: list[tuple[float, dict, dict]] = []
    used_question_ids = {row["question_id"] for row in sample_rows}
    for oracle in oracle_rows:
        full = full_by_qid.get(oracle["question_id"])
        if full is None or oracle["question_id"] in used_question_ids:
            continue
        abstain_diff = float(bool(oracle.get("abstained")) != bool(full.get("abstained")))
        norm_diff = float(oracle.get("normalized_em", 0.0) != full.get("normalized_em", 0.0))
        evidence_diff = abs(float(oracle.get("evidence_use", 0.0)) - float(full.get("evidence_use", 0.0)))
        f1_diff = abs(float(oracle.get("token_f1", 0.0)) - float(full.get("token_f1", 0.0)))
        score = 2.0 * abstain_diff + norm_diff + evidence_diff + f1_diff
        if score > 0.0:
            disagreement_rows.append((score, oracle, full))
    disagreement_rows.sort(key=lambda item: (-item[0], item[1].get("question_type", ""), item[1].get("question_id", "")))
    selected_pairs = disagreement_rows[:10]
    category_counts[category] = len(selected_pairs)
    for _score, oracle, full in selected_pairs:
        sample_rows.append(
            sample_payload(
                oracle,
                category,
                contexts,
                examples_by_id,
                retrieval_by_method,
                budget_frac,
                max_context_words,
                max_memory_chars,
                paired_row=full,
            )
        )

    return sample_rows, category_counts


def normalization_deltas(rows: list[dict], limit: int = 25) -> list[dict]:
    changed = [
        row
        for row in rows
        if row.get("normalized_em", 0.0) > row.get("raw_em", 0.0)
    ]
    changed.sort(key=lambda row: (row.get("method", ""), row.get("question_id", "")))
    return [
        {
            "question_id": row.get("question_id"),
            "question_type": row.get("question_type"),
            "answer_type": row.get("answer_type"),
            "method": row.get("method"),
            "method_label": row.get("method_label"),
            "gold": row.get("gold"),
            "prediction": row.get("prediction"),
            "raw_em": row.get("raw_em"),
            "normalized_em": row.get("normalized_em"),
            "token_f1": row.get("token_f1"),
        }
        for row in changed[:limit]
    ]


def write_jsonl(path: Path, rows: list[dict]) -> None:
    with path.open("w", encoding="utf-8") as handle:
        for row in rows:
            handle.write(json.dumps(row, ensure_ascii=True) + "\n")


def format_rate(value: float) -> str:
    return f"{value:.4f}"


def write_report(path: Path, audit: dict) -> None:
    lines = [
        "# GPT-5.5 Scoring Audit",
        "",
        f"- Input run: `{audit['input_run_dir']}`",
        f"- Rows audited: `{audit['n_rows']}`",
        "- Scope: existing frozen-context GPT-5.5 reader outputs only; no new model calls.",
        "- Optional semantic judge: not run, because no cached judge outputs were present and the task asked not to spend on full benchmark judging.",
        "",
        "## Normalized Scoring",
        "",
        "Normalized EM lowercases, strips punctuation and articles, collapses whitespace, canonicalizes simple date mentions, maps number words zero to twenty to digits, and handles a small alias set (US/USA, UK, NYC). Gold labels with explicit acceptable alternatives are split into deterministic variants.",
        "",
        "| Method | Raw EM | Normalized EM | Token F1 | Evidence use | Insuff. | Gold retrieved |",
        "|---|---:|---:|---:|---:|---:|---:|",
    ]
    for method, row in audit["method_summary"].items():
        focus = row["focus"]
        lines.append(
            f"| {row['method_label']} | {format_rate(focus['raw_em'])} | "
            f"{format_rate(focus['normalized_em'])} | {format_rate(focus['token_f1'])} | "
            f"{format_rate(focus['evidence_use'])} | {format_rate(focus['insufficient_evidence_rate'])} | "
            f"{format_rate(focus['gold_evidence_retrieved'])} |"
        )

    lines.extend(
        [
            "",
            "## Answer-Type Analysis",
            "",
            "| Method | Answer type | n | Raw EM | Normalized EM | Token F1 | Evidence use | Insuff. |",
            "|---|---|---:|---:|---:|---:|---:|---:|",
        ]
    )
    for _method, method_row in audit["answer_type_analysis"].items():
        for answer_type, metrics in method_row["answer_types"].items():
            lines.append(
                f"| {method_row['method_label']} | {answer_type} | {metrics['n']} | "
                f"{format_rate(metrics['raw_em'])} | {format_rate(metrics['normalized_em'])} | "
                f"{format_rate(metrics['token_f1'])} | {format_rate(metrics['evidence_use'])} | "
                f"{format_rate(metrics['insufficient_evidence_rate'])} |"
            )

    lines.extend(
        [
            "",
            "## Balanced Audit Sample",
            "",
            f"- Sample path: `{audit['sample_path']}`",
            f"- Total rows: `{audit['sample_summary']['n']}`",
            "",
            "| Category | Rows |",
            "|---|---:|",
        ]
    )
    for category, count in audit["sample_summary"]["category_counts"].items():
        lines.append(f"| `{category}` | {count} |")

    delta_count = len(audit["normalization_delta_examples"])
    lines.extend(
        [
            "",
            "## Interpretation",
            "",
            f"- Normalized EM changes {audit['normalization_changed_count']} of {audit['n_rows']} method-question rows; the first {delta_count} changed examples are stored in `normalized_scoring_v2.json`.",
            "- Normalization materially raises absolute EM for OracleMem and full raw, mainly for explicit acceptable duration labels and number-word/date formatting.",
            "- The OracleMem/full-raw normalized-EM gap remains modest; the strongest external signal is still OracleMem's higher token-F1 and evidence-use.",
            "- The balanced sample is intended for a human or cheap blinded judge pass: it separates supported abstentions, high-overlap EM failures, full-raw EM failures, and OracleMem/full-raw disagreements.",
        ]
    )
    path.write_text("\n".join(lines) + "\n", encoding="utf-8")


def main() -> None:
    parser = argparse.ArgumentParser(description="Audit GPT-5.5 LongMemEval reader scoring.")
    parser.add_argument("--run-dir", type=Path, default=DEFAULT_RUN_DIR)
    parser.add_argument("--out-dir", type=Path, default=DEFAULT_OUT_DIR)
    parser.add_argument("--dataset-json", type=Path, default=DEFAULT_DATASET)
    parser.add_argument("--retrieval-rows-json", type=Path, default=DEFAULT_RETRIEVAL_ROWS)
    parser.add_argument("--budget-frac", type=float, default=0.20)
    parser.add_argument("--max-context-words", type=int, default=1800)
    parser.add_argument("--max-memory-chars", type=int, default=900)
    parser.add_argument("--focus-types", type=str, default=",".join(sorted(FOCUS_TYPES)))
    args = parser.parse_args()

    focus_types = {part.strip() for part in args.focus_types.split(",") if part.strip()}
    args.out_dir.mkdir(parents=True, exist_ok=True)

    examples = load_examples(args.dataset_json, None)
    examples_by_id = {example["question_id"]: example for example in examples}
    reader_rows = load_reader_outputs(args.run_dir)
    enriched = enrich_rows(reader_rows, examples_by_id)

    retrieval_rows = json.loads(args.retrieval_rows_json.read_text(encoding="utf-8"))
    retrieval_by_method = retrieval_lookup(retrieval_rows)
    contexts: dict[tuple[str, str], list[ContextEntry]] = {}
    sample, category_counts = build_balanced_sample(
        enriched,
        contexts,
        examples_by_id,
        retrieval_by_method,
        args.budget_frac,
        args.max_context_words,
        args.max_memory_chars,
    )

    sample_path = args.out_dir / "semantic_audit_sample_50.jsonl"
    write_jsonl(sample_path, sample)

    deltas = normalization_deltas(enriched)
    audit = {
        "input_run_dir": str(args.run_dir),
        "dataset_json": str(args.dataset_json),
        "retrieval_rows_json": str(args.retrieval_rows_json),
        "n_rows": len(enriched),
        "focus_types": sorted(focus_types),
        "normalization_definition": {
            "lowercase": True,
            "strip_punctuation": True,
            "strip_articles": sorted(ARTICLES),
            "collapse_whitespace": True,
            "date_normalization": "month-name dates and simple slash/dash dates are canonicalized when detectable",
            "number_word_normalization": "zero through twenty are mapped to digits",
            "aliases": ["US/USA -> United States", "UK -> United Kingdom", "New York City -> NYC"],
            "gold_variants": "explicit acceptable alternatives and parenthetical-free variants are considered",
        },
        "method_summary": method_summary(enriched, focus_types),
        "answer_type_analysis": answer_type_summary(enriched),
        "normalization_changed_count": sum(
            1 for row in enriched if row.get("normalized_em", 0.0) > row.get("raw_em", 0.0)
        ),
        "normalization_delta_examples": deltas,
        "sample_summary": {
            "n": len(sample),
            "category_counts": category_counts,
            "balance_target": {
                "oraclemem_abstained_despite_support": 20,
                "oraclemem_high_f1_em0": 10,
                "full_raw_high_f1_em0": 10,
                "oraclemem_full_raw_disagreement": 10,
            },
        },
        "sample_path": str(sample_path),
        "semantic_judge": {
            "used": False,
            "reason": "No cached judge outputs were present; no new API judge calls were made.",
        },
    }

    json_path = args.out_dir / "normalized_scoring_v2.json"
    json_path.write_text(json.dumps(audit, indent=2, ensure_ascii=True), encoding="utf-8")
    write_report(args.out_dir / "SCORING_AUDIT.md", audit)
    print(json.dumps({"wrote": [str(json_path), str(sample_path), str(args.out_dir / "SCORING_AUDIT.md")]}, indent=2))


if __name__ == "__main__":
    main()