File size: 28,751 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
"""Adjudicate a natural OracleMem coverage package with a separate LLM judge.

The Natural-200 package is useful only if its evidence-unit labels and coverage
edges are semantically stable.  This script builds a smaller adjudicated package
from an existing natural package:

* candidate memories are copied from the primary package;
* a separate Gemini Flash adjudicator reviews required evidence units and
  candidate-unit coverage edges;
* only accepted/corrected adjudications are exported into a new coverage
  package;
* exact package-OPT and baseline scores are recomputed on the adjudicated
  package.

This is not human adjudication.  It is an intermediate validity check that is
cheaper than human review and more useful than treating primary annotation as
ground truth.
"""

from __future__ import annotations

import argparse
import json
import math
import os
import random
import statistics
import time
from collections import defaultdict
from pathlib import Path
import sys
from typing import Any, Iterable, Mapping, Sequence

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

from oraclemem.evaluate import evaluate_instance, write_benchmark_outputs

from llm_memory_validation.gemini_natural_oraclemem import (
    OpenRouterJsonClient,
    load_env_file,
    stable_hash,
    truncate_words,
    word_count,
)
from llm_memory_validation.run_mem0_natural_baseline import (
    PackageData,
    load_package,
    package_instance,
    prefix_of,
    read_jsonl,
    write_json,
    write_jsonl,
)


DEFAULT_ADJUDICATOR_MODEL = "google/gemini-2.5-flash"
DEFAULT_METHODS = (
    "opt",
    "oracle_gvt",
    "summary_only",
    "fact_only",
    "mem0_extract",
    "amem_graph",
    "recency_raw",
    "estimated_gvt",
)


def mean(values: Sequence[float]) -> float | None:
    clean = [float(value) for value in values if value is not None and math.isfinite(float(value))]
    if not clean:
        return None
    return statistics.fmean(clean)


def read_disagreement_ids(path: Path | None) -> set[str]:
    if path is None or not path.exists():
        return set()
    ids: set[str] = set()
    for row in read_jsonl(path):
        label = str(row.get("agreement_label", row.get("status", ""))).lower()
        if "major" in label or "disagreement" in label or "unresolved" in label:
            query_id = row.get("query_id")
            if query_id:
                ids.add(str(query_id))
    return ids


def read_mem0_gap_by_instance(path: Path | None) -> dict[str, float]:
    if path is None or not path.exists():
        return {}
    by_instance_budget: dict[tuple[str, int], dict[str, float]] = defaultdict(dict)
    for row in read_jsonl(path):
        ratio = row.get("package_oracle_ratio")
        if ratio is None:
            continue
        key = (str(row.get("instance_id")), int(row.get("budget", 0) or 0))
        by_instance_budget[key][str(row.get("method"))] = float(ratio)
    gaps: dict[str, list[float]] = defaultdict(list)
    for (instance_id, _budget), scores in by_instance_budget.items():
        if "actual_mem0_oracle_pruned_upper" not in scores or "actual_mem0_recency_pruned" not in scores:
            continue
        gaps[instance_id].append(
            max(0.0, scores["actual_mem0_oracle_pruned_upper"] - scores["actual_mem0_recency_pruned"])
        )
    return {instance_id: statistics.fmean(values) for instance_id, values in gaps.items() if values}


def select_queries(
    queries: Sequence[Mapping[str, Any]],
    *,
    limit: int,
    disagreement_ids: set[str],
    mem0_gap_by_instance: Mapping[str, float],
    seed: int,
) -> list[dict[str, Any]]:
    """Select a deterministic stratified subset for adjudication."""

    rng = random.Random(seed)
    eligible = [dict(row) for row in queries if row.get("required_unit_ids")]
    by_id = {str(row["query_id"]): row for row in eligible}
    selected_ids: list[str] = []

    def add(query_id: str) -> None:
        if query_id in by_id and query_id not in selected_ids and len(selected_ids) < limit:
            selected_ids.append(query_id)

    # First include examples where the previous independent annotation disagreed.
    for query_id in sorted(disagreement_ids):
        add(query_id)

    # Then include examples where Mem0 extraction and budget selection diverged.
    for query_id, _gap in sorted(mem0_gap_by_instance.items(), key=lambda item: (-item[1], item[0])):
        add(query_id)

    # Ensure category diversity.
    categories: dict[str, list[str]] = defaultdict(list)
    for row in eligible:
        categories[str(row.get("category", "unknown"))].append(str(row["query_id"]))
    for ids in categories.values():
        rng.shuffle(ids)
    while len(selected_ids) < min(limit, len(eligible)):
        made_progress = False
        for category in sorted(categories):
            while categories[category]:
                query_id = categories[category].pop()
                if query_id not in selected_ids:
                    add(query_id)
                    made_progress = True
                    break
            if len(selected_ids) >= limit:
                break
        if not made_progress:
            break

    # Fill any remaining slots randomly but deterministically.
    remaining = [str(row["query_id"]) for row in eligible if str(row["query_id"]) not in selected_ids]
    rng.shuffle(remaining)
    for query_id in remaining:
        add(query_id)

    return [dict(by_id[query_id]) for query_id in selected_ids]


def unit_rows_for_query(data: PackageData, query_id: str) -> list[dict[str, Any]]:
    rows = list(data.evidence_by_instance.get(query_id, []))
    rows.sort(key=lambda row: str(row.get("unit_id", "")))
    return rows


def candidate_rows_for_query(data: PackageData, query_id: str) -> list[dict[str, Any]]:
    rows = list(data.candidate_rows_by_instance.get(query_id, []))
    rows.sort(
        key=lambda row: (
            int(row.get("time_index", 0) or 0),
            str(row.get("experience_id", "")),
            int(row.get("cost", row.get("cost_tokens", 0)) or 0),
            str(row.get("candidate_id", "")),
        )
    )
    return rows


def compact_experience_rows(data: PackageData, query_id: str, max_words: int) -> list[dict[str, Any]]:
    rows = []
    for row in sorted(data.experiences_by_instance.get(query_id, []), key=lambda item: str(item.get("experience_id", ""))):
        text = str(row.get("text", ""))
        rows.append(
            {
                "experience_id": row.get("experience_id"),
                "source_kind": row.get("source_kind"),
                "timestamp": row.get("timestamp"),
                "text": truncate_words(text, max_words),
            }
        )
    return rows


def adjudication_prompt(
    *,
    query: Mapping[str, Any],
    evidence_units: Sequence[Mapping[str, Any]],
    candidate_rows: Sequence[Mapping[str, Any]],
    experiences: Sequence[Mapping[str, Any]],
    max_candidate_words: int,
) -> str:
    units = [
        {
            "unit_id": row.get("unit_id"),
            "kind": row.get("kind"),
            "canonical_text": row.get("canonical_text"),
            "primary_required": str(row.get("unit_id")) in set(query.get("required_unit_ids", []) or []),
            "primary_unit_weight": float(row.get("unit_weight", 0.0) or 0.0),
            "source_quotes": [
                truncate_words(str(span.get("text", "")), 80)
                for span in row.get("source_spans", []) or []
                if isinstance(span, Mapping)
            ][:2],
        }
        for row in evidence_units
    ]
    candidates = [
        {
            "candidate_id": row.get("candidate_id"),
            "experience_id": row.get("experience_id"),
            "representation_type": row.get("representation_type"),
            "generator_id": row.get("generator_id", row.get("generator")),
            "cost": int(row.get("cost", row.get("cost_tokens", 1)) or 1),
            "text": truncate_words(str(row.get("serialized") or row.get("text") or ""), max_candidate_words),
        }
        for row in candidate_rows
    ]
    payload = {
        "query_id": query.get("query_id"),
        "question": query.get("question"),
        "gold_answer": query.get("answer"),
        "category": query.get("category"),
        "primary_required_unit_ids": query.get("required_unit_ids", []),
        "primary_annotation_rationale": query.get("annotation_rationale", ""),
        "support_experiences": experiences,
        "evidence_units": units,
        "candidate_memories": candidates,
    }
    return (
        "You are adjudicating an OracleMem natural-trace coverage package.\n"
        "Your job is to produce conservative benchmark labels. Use the question and gold answer only for adjudication.\n"
        "Do not create new evidence unit ids. Select only from the existing evidence_units.\n"
        "First choose the minimal existing evidence_unit ids needed to answer the question exactly.\n"
        "Then map candidate memories to evidence units only when the candidate text entails the unit.\n"
        "Coverage values: 1.0 for complete entailment, 0.5 for partial but useful entailment. Omit unsupported pairs.\n"
        "If the existing units are insufficient, mark status='rejected'. If the answer is ambiguous, mark status='ambiguous'.\n"
        "If the primary labels are basically correct, mark status='accepted'. If you change required units or coverage, mark status='corrected'.\n"
        "Return strict JSON only with this schema:\n"
        "{\n"
        '  "status": "accepted|corrected|ambiguous|rejected",\n'
        '  "required_unit_ids": ["..."],\n'
        '  "coverage_edges": [\n'
        '    {"candidate_id": "...", "unit_id": "...", "coverage": 1.0, "rationale": "..."}\n'
        "  ],\n"
        '  "confidence": 0.0,\n'
        '  "rationale": "..."\n'
        "}\n\n"
        f"PACKAGE:\n{json.dumps(payload, indent=2, sort_keys=True)}"
    )


def clean_adjudication(
    *,
    parsed: Mapping[str, Any],
    query: Mapping[str, Any],
    evidence_units: Sequence[Mapping[str, Any]],
    candidate_rows: Sequence[Mapping[str, Any]],
) -> dict[str, Any]:
    allowed_units = {str(row.get("unit_id")) for row in evidence_units}
    allowed_candidates = {str(row.get("candidate_id")) for row in candidate_rows}
    primary_required = set(str(unit_id) for unit_id in query.get("required_unit_ids", []) or [])
    status = str(parsed.get("status", "")).strip().lower()
    if status not in {"accepted", "corrected", "ambiguous", "rejected"}:
        status = "corrected"

    required = []
    for unit_id in parsed.get("required_unit_ids", []) or []:
        unit_id = str(unit_id)
        if unit_id in allowed_units and unit_id not in required:
            required.append(unit_id)
    if status in {"accepted", "corrected"} and not required:
        status = "rejected"

    edges: list[dict[str, Any]] = []
    seen_edges: set[tuple[str, str]] = set()
    for edge in parsed.get("coverage_edges", []) or []:
        if not isinstance(edge, Mapping):
            continue
        candidate_id = str(edge.get("candidate_id", ""))
        unit_id = str(edge.get("unit_id", ""))
        if candidate_id not in allowed_candidates or unit_id not in allowed_units:
            continue
        coverage = max(0.0, min(1.0, float(edge.get("coverage", edge.get("fidelity", 0.0)) or 0.0)))
        if coverage <= 0:
            continue
        key = (candidate_id, unit_id)
        if key in seen_edges:
            continue
        seen_edges.add(key)
        edges.append(
            {
                "candidate_id": candidate_id,
                "unit_id": unit_id,
                "coverage": coverage,
                "coverage_label": "full" if coverage >= 0.999 else "partial",
                "rationale": str(edge.get("rationale", "")),
            }
        )

    if status == "accepted" and set(required) != primary_required:
        status = "corrected"
    confidence = max(0.0, min(1.0, float(parsed.get("confidence", 0.0) or 0.0)))
    return {
        "query_id": str(query.get("query_id")),
        "status": status,
        "required_unit_ids": required,
        "coverage_edges": edges,
        "confidence": confidence,
        "rationale": str(parsed.get("rationale", "")),
        "primary_required_unit_ids": sorted(primary_required),
        "required_changed": sorted(primary_required) != sorted(required),
    }


def export_adjudicated_package(
    *,
    primary_data: PackageData,
    accepted_queries: Sequence[Mapping[str, Any]],
    adjudications: Mapping[str, Mapping[str, Any]],
    out_dir: Path,
    adjudicator_model: str,
    primary_package_dir: Path,
) -> None:
    package_dir = out_dir / "coverage_package"
    package_dir.mkdir(parents=True, exist_ok=True)

    accepted_ids = {str(query["query_id"]) for query in accepted_queries}
    experience_rows = [
        row
        for query_id in accepted_ids
        for row in primary_data.experiences_by_instance.get(query_id, [])
    ]
    candidate_rows = [
        row
        for query_id in accepted_ids
        for row in primary_data.candidate_rows_by_instance.get(query_id, [])
    ]

    evidence_rows: list[dict[str, Any]] = []
    query_rows: list[dict[str, Any]] = []
    coverage_rows: list[dict[str, Any]] = []
    decision_rows: list[dict[str, Any]] = []
    for query in accepted_queries:
        query_id = str(query["query_id"])
        adjudication = adjudications[query_id]
        required = set(str(unit_id) for unit_id in adjudication.get("required_unit_ids", []) or [])
        for row in primary_data.evidence_by_instance.get(query_id, []):
            updated = dict(row)
            updated["unit_weight"] = 1.0 if str(updated.get("unit_id")) in required else 0.0
            updated["adjudication_status"] = "model_adjudicated"
            updated["annotator_ids"] = list(dict.fromkeys([*(updated.get("annotator_ids", []) or []), adjudicator_model]))
            evidence_rows.append(updated)
        updated_query = dict(query)
        updated_query["primary_required_unit_ids"] = list(query.get("required_unit_ids", []) or [])
        updated_query["required_unit_ids"] = sorted(required)
        updated_query["annotation_rationale"] = str(adjudication.get("rationale", ""))
        updated_query["adjudication_status"] = str(adjudication.get("status"))
        updated_query["adjudicator_model"] = adjudicator_model
        query_rows.append(updated_query)
        for edge in adjudication.get("coverage_edges", []) or []:
            coverage_rows.append(
                {
                    "candidate_id": edge["candidate_id"],
                    "unit_id": edge["unit_id"],
                    "coverage": edge["coverage"],
                    "coverage_label": edge["coverage_label"],
                    "rationale": edge["rationale"],
                    "adjudication_status": "model_adjudicated",
                    "annotator_ids": [adjudicator_model],
                    "experience_id": str(edge["candidate_id"]).rsplit("::", 1)[0],
                    "candidate_group": str(edge["candidate_id"]).rsplit("::", 1)[0],
                }
            )
        decision_rows.append(dict(adjudication))

    write_jsonl(package_dir / "experiences.jsonl", experience_rows)
    write_jsonl(package_dir / "evidence_units.jsonl", evidence_rows)
    write_jsonl(package_dir / "queries.jsonl", query_rows)
    write_jsonl(package_dir / "candidate_memories.jsonl", candidate_rows)
    write_jsonl(package_dir / "coverage_matrix.jsonl", coverage_rows)
    write_jsonl(package_dir / "annotation_decisions.jsonl", decision_rows)

    file_hashes = {}
    for name in (
        "experiences.jsonl",
        "evidence_units.jsonl",
        "queries.jsonl",
        "candidate_memories.jsonl",
        "coverage_matrix.jsonl",
        "annotation_decisions.jsonl",
    ):
        file_hashes[name] = stable_hash((package_dir / name).read_text(encoding="utf-8"))

    manifest = {
        "schema_version": 1,
        "package_kind": "natural_adjudicated_subset",
        "primary_package_dir": str(primary_package_dir),
        "adjudicator_model": adjudicator_model,
        "counts": {
            "instances": len(query_rows),
            "experiences": len(experience_rows),
            "evidence_units": len(evidence_rows),
            "candidate_memories": len(candidate_rows),
            "positive_coverage_rows": len(coverage_rows),
            "queries": len(query_rows),
        },
        "allowed_inputs": [
            "primary package support-slice experiences",
            "primary package evidence units and candidates",
            "question and gold answer for adjudication only",
        ],
        "forbidden_inputs_for_candidate_generation": [
            "adjudicated required_unit_ids",
            "adjudicated coverage edges",
            "solver outputs",
        ],
        "limitations": [
            "LLM adjudicated, not human adjudicated",
            "support-sliced, not full-haystack",
            "exact OPT is finite package OPT over copied primary candidates",
        ],
        "file_hashes": file_hashes,
    }
    write_json(package_dir / "candidate_generation_manifest.json", manifest)
    (package_dir / "README.md").write_text(
        "# OracleMem Natural Adjudicated Coverage Package\n\n"
        "This package is a model-adjudicated subset exported from the primary Natural package. "
        "It is intended as a semantic-stability diagnostic, not as human ground truth.\n",
        encoding="utf-8",
    )


def evaluate_package(
    package_dir: Path,
    budgets: Sequence[int],
    methods: Sequence[str],
    out_dir: Path,
    *,
    estimator_model: str,
) -> dict[str, str]:
    data = load_package(package_dir)
    results = []
    for query in data.queries:
        instance = package_instance(data, query)
        results.extend(
            evaluate_instance(
                instance,
                budgets,
                methods=methods,
                estimator_model=estimator_model,
                estimator_profile="gemini_flash_lite_v1",
            )
        )
    return write_benchmark_outputs(results, out_dir)


def write_report(
    *,
    out_dir: Path,
    selected_queries: Sequence[Mapping[str, Any]],
    accepted_queries: Sequence[Mapping[str, Any]],
    rejected_queries: Sequence[Mapping[str, Any]],
    adjudications: Mapping[str, Mapping[str, Any]],
    benchmark_summary_path: Path | None,
    model: str,
    usage_rows: Sequence[Mapping[str, Any]],
) -> None:
    status_counts: dict[str, int] = defaultdict(int)
    changed = 0
    for adj in adjudications.values():
        status_counts[str(adj.get("status", "unknown"))] += 1
        changed += int(bool(adj.get("required_changed")))
    usage_totals: dict[str, float] = defaultdict(float)
    for row in usage_rows:
        usage = row.get("usage", {}) if isinstance(row, Mapping) else {}
        if not isinstance(usage, Mapping):
            continue
        for key in ("prompt_tokens", "completion_tokens", "total_tokens", "cost"):
            try:
                usage_totals[key] += float(usage.get(key, 0.0) or 0.0)
            except (TypeError, ValueError):
                pass

    summary = {
        "model": model,
        "attempted": len(selected_queries),
        "accepted_or_corrected": len(accepted_queries),
        "rejected_or_ambiguous": len(rejected_queries),
        "status_counts": dict(sorted(status_counts.items())),
        "required_changed_n": changed,
        "required_changed_rate": changed / max(1, len(adjudications)),
        "usage": dict(sorted(usage_totals.items())),
        "benchmark_summary_path": str(benchmark_summary_path) if benchmark_summary_path else None,
    }
    write_json(out_dir / "adjudication_summary.json", summary)

    lines = [
        "# Natural Package Adjudication Report",
        "",
        f"- Adjudicator model: `{model}`",
        f"- Attempted examples: {summary['attempted']}",
        f"- Accepted/corrected examples exported: {summary['accepted_or_corrected']}",
        f"- Rejected/ambiguous examples: {summary['rejected_or_ambiguous']}",
        f"- Required-unit changed rate: {summary['required_changed_rate']:.3f}",
        f"- API total tokens: {usage_totals.get('total_tokens', 0.0):.0f}",
        f"- API cost reported by OpenRouter: ${usage_totals.get('cost', 0.0):.4f}",
        "",
        "## Status Counts",
        "",
    ]
    for status, count in sorted(status_counts.items()):
        lines.append(f"- `{status}`: {count}")
    if benchmark_summary_path and benchmark_summary_path.exists():
        benchmark = json.loads(benchmark_summary_path.read_text(encoding="utf-8"))
        lines.extend(
            [
                "",
                "## Adjudicated Package Scores",
                "",
                "| Budget | Method | N | Mean ratio to exact package OPT | Bootstrap 95% CI |",
                "|---:|---|---:|---:|---|",
            ]
        )
        for row in benchmark.get("by_budget_method", []):
            lines.append(
                "| {budget} | `{method}` | {n} | {ratio:.3f} | [{lo:.3f}, {hi:.3f}] |".format(
                    budget=row.get("budget"),
                    method=row.get("method"),
                    n=row.get("n"),
                    ratio=row.get("mean_ratio_to_opt", float("nan")),
                    lo=row.get("bootstrap95_ratio_to_opt_low", float("nan")),
                    hi=row.get("bootstrap95_ratio_to_opt_high", float("nan")),
                )
            )
    lines.extend(
        [
            "",
            "## Claim Boundary",
            "",
            "This is model adjudication with Gemini Flash, not human ground truth. It is useful as a stricter semantic-stability diagnostic than the primary single-annotator package, but any main-paper claim should still call it model-adjudicated rather than human-adjudicated.",
        ]
    )
    (out_dir / "REPORT.md").write_text("\n".join(lines) + "\n", encoding="utf-8")


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--primary-package-dir", type=Path, required=True)
    parser.add_argument("--out-dir", type=Path, required=True)
    parser.add_argument("--api-env", type=Path, default=Path("api.env"))
    parser.add_argument("--model", default=DEFAULT_ADJUDICATOR_MODEL)
    parser.add_argument("--limit", type=int, default=50)
    parser.add_argument("--budgets", default="30,60,100")
    parser.add_argument("--methods", default=",".join(DEFAULT_METHODS))
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--secondary-agreement-rows", type=Path, default=None)
    parser.add_argument("--mem0-raw-results", type=Path, default=None)
    parser.add_argument("--max-experience-words", type=int, default=900)
    parser.add_argument("--max-candidate-words", type=int, default=220)
    parser.add_argument("--request-sleep", type=float, default=0.02)
    parser.add_argument("--skip-existing", action="store_true")
    args = parser.parse_args()

    env_values = load_env_file(args.api_env)
    for key, value in env_values.items():
        os.environ.setdefault(key, value)
    if not os.environ.get("OPENROUTER_API_KEY"):
        raise RuntimeError("OPENROUTER_API_KEY is required in the environment or api.env")

    args.out_dir.mkdir(parents=True, exist_ok=True)
    data = load_package(args.primary_package_dir)
    disagreement_ids = read_disagreement_ids(args.secondary_agreement_rows)
    mem0_gap_by_instance = read_mem0_gap_by_instance(args.mem0_raw_results)
    selected_queries = select_queries(
        data.queries,
        limit=args.limit,
        disagreement_ids=disagreement_ids,
        mem0_gap_by_instance=mem0_gap_by_instance,
        seed=args.seed,
    )
    write_jsonl(args.out_dir / "selected_queries.jsonl", selected_queries)

    client = OpenRouterJsonClient(
        api_key=os.environ["OPENROUTER_API_KEY"],
        model=args.model,
        cache_path=args.out_dir / "openrouter_cache_adjudication.json",
        max_tokens=3500,
        request_sleep=args.request_sleep,
    )

    usage_rows: list[dict[str, Any]] = []
    adjudications: dict[str, dict[str, Any]] = {}
    raw_rows: list[dict[str, Any]] = []
    for index, query in enumerate(selected_queries, start=1):
        query_id = str(query["query_id"])
        marker = args.out_dir / "per_instance" / f"{query_id}.done.json"
        if args.skip_existing and marker.exists():
            cached = json.loads(marker.read_text(encoding="utf-8"))
            adjudications[query_id] = cached["adjudication"]
            continue
        evidence_units = unit_rows_for_query(data, query_id)
        candidate_rows = candidate_rows_for_query(data, query_id)
        experiences = compact_experience_rows(data, query_id, args.max_experience_words)
        started = time.perf_counter()
        response = client(
            adjudication_prompt(
                query=query,
                evidence_units=evidence_units,
                candidate_rows=candidate_rows,
                experiences=experiences,
                max_candidate_words=args.max_candidate_words,
            ),
            purpose="natural_package_adjudication",
        )
        parsed = response.get("parsed", {}) if isinstance(response, Mapping) else {}
        adjudication = clean_adjudication(
            parsed=parsed,
            query=query,
            evidence_units=evidence_units,
            candidate_rows=candidate_rows,
        )
        adjudication.update(
            {
                "model": args.model,
                "prompt_hash": response.get("prompt_hash"),
                "cache_hit": response.get("cache_hit"),
                "runtime_sec": time.perf_counter() - started,
                "selected_index": index,
            }
        )
        adjudications[query_id] = adjudication
        usage_rows.append(
            {
                "query_id": query_id,
                "prompt_hash": response.get("prompt_hash"),
                "usage": response.get("usage", {}),
                "cache_hit": response.get("cache_hit"),
            }
        )
        raw_rows.append(
            {
                "query_id": query_id,
                "response": response,
                "adjudication": adjudication,
            }
        )
        marker.parent.mkdir(parents=True, exist_ok=True)
        write_json(marker, {"query_id": query_id, "adjudication": adjudication})

    write_jsonl(args.out_dir / "adjudication_raw.jsonl", raw_rows)
    write_jsonl(args.out_dir / "api_usage.jsonl", usage_rows)
    write_jsonl(args.out_dir / "adjudication_decisions.jsonl", list(adjudications.values()))

    accepted_queries = [
        query
        for query in selected_queries
        if str(adjudications.get(str(query["query_id"]), {}).get("status")) in {"accepted", "corrected"}
    ]
    rejected_queries = [query for query in selected_queries if query not in accepted_queries]
    export_adjudicated_package(
        primary_data=data,
        accepted_queries=accepted_queries,
        adjudications=adjudications,
        out_dir=args.out_dir,
        adjudicator_model=args.model,
        primary_package_dir=args.primary_package_dir,
    )

    budgets = [int(float(item.strip())) for item in args.budgets.split(",") if item.strip()]
    methods = tuple(args.methods.replace(",", " ").split())
    benchmark_paths: dict[str, str] | None = None
    if accepted_queries:
        benchmark_paths = evaluate_package(
            args.out_dir / "coverage_package",
            budgets,
            methods,
            args.out_dir,
            estimator_model=args.model,
        )

    write_report(
        out_dir=args.out_dir,
        selected_queries=selected_queries,
        accepted_queries=accepted_queries,
        rejected_queries=rejected_queries,
        adjudications=adjudications,
        benchmark_summary_path=Path(benchmark_paths["summary_json"]) if benchmark_paths else None,
        model=args.model,
        usage_rows=usage_rows,
    )
    print(
        json.dumps(
            {
                "out_dir": str(args.out_dir),
                "attempted": len(selected_queries),
                "accepted_or_corrected": len(accepted_queries),
                "rejected_or_ambiguous": len(rejected_queries),
                "model": args.model,
                "benchmark_summary": benchmark_paths["summary_json"] if benchmark_paths else None,
            },
            indent=2,
            sort_keys=True,
        )
    )


if __name__ == "__main__":
    main()