File size: 51,933 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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
"""Build a Gemini-annotated natural OracleMem pilot from LongMemEval-S.



This script is intentionally separate from the synthetic OracleMem runner.  It

uses Gemini through OpenRouter to create an auditable natural-trace coverage

package:



* candidate memories are generated from conversation sessions only;

* query/gold answers are used only in a separate annotation step that maps

  extracted evidence units to the evaluation question;

* exact OPT is solved over the resulting finite candidate set;

* local published-system-inspired writer policies are scored under the same

  candidate set and budget.



The default run is a small pilot.  Scale `--limit` only after checking cache hit

rate, cost, and package quality.

"""

from __future__ import annotations

import argparse
import hashlib
import json
import math
import random
import re
import statistics
import string
import time
import urllib.error
import urllib.request
from collections import defaultdict
from dataclasses import asdict, dataclass
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 (
    CandidateMemory,
    OracleMemInstance,
    SelectionResult,
    evaluate_instance,
    write_benchmark_outputs,
)


FOCUS_TYPES = {"knowledge-update", "temporal-reasoning"}
DEFAULT_MODEL = "google/gemini-3.1-flash-lite-preview"
DEFAULT_METHODS = (
    "opt",
    "oracle_gvt",
    "memgpt_tiered",
    "mem0_extract",
    "amem_graph",
    "amac_admission",
    "recency_raw",
    "summary_only",
    "fact_only",
)


def load_env_file(path: Path) -> dict[str, str]:
    values: dict[str, str] = {}
    if not path.exists():
        return values
    for line in path.read_text(encoding="utf-8").splitlines():
        stripped = line.strip()
        if not stripped or stripped.startswith("#") or "=" not in stripped:
            continue
        key, value = stripped.split("=", 1)
        values[key.strip()] = value.strip().strip('"').strip("'")
    return values


def stable_hash(text: str) -> str:
    return hashlib.sha256(text.encode("utf-8")).hexdigest()


def safe_token(value: str) -> str:
    cleaned = "".join(char if char.isalnum() or char in "._-" else "_" for char in value)
    return cleaned.strip("._") or "item"


def word_count(text: str) -> int:
    return len(re.findall(r"\S+", text))


def truncate_words(text: str, limit: int) -> str:
    words = re.findall(r"\S+", text)
    if len(words) <= limit:
        return text
    return " ".join(words[:limit]) + " ..."


def extract_json_object(text: str | None) -> dict[str, Any]:
    if not text:
        return {}
    stripped = text.strip()
    try:
        parsed = json.loads(stripped)
        return parsed if isinstance(parsed, dict) else {}
    except json.JSONDecodeError:
        pass
    match = re.search(r"\{.*\}", stripped, flags=re.DOTALL)
    if not match:
        return {}
    try:
        parsed = json.loads(match.group(0))
    except json.JSONDecodeError:
        return {}
    return parsed if isinstance(parsed, dict) else {}


class OpenRouterJsonClient:
    """Small cached OpenRouter JSON client for Gemini annotation."""

    def __init__(

        self,

        *,

        api_key: str,

        model: str,

        cache_path: Path,

        max_tokens: int = 1400,

        temperature: float = 0.0,

        timeout: int = 120,

        request_sleep: float = 0.02,

    ) -> None:
        self.api_key = api_key
        self.model = model
        self.cache_path = cache_path
        self.max_tokens = max_tokens
        self.temperature = temperature
        self.timeout = timeout
        self.request_sleep = request_sleep
        self.cache: dict[str, dict[str, Any]] = {}
        if cache_path.exists():
            self.cache = json.loads(cache_path.read_text(encoding="utf-8"))

    def _write_cache(self) -> None:
        self.cache_path.parent.mkdir(parents=True, exist_ok=True)
        self.cache_path.write_text(json.dumps(self.cache, indent=2, sort_keys=True), encoding="utf-8")

    def __call__(self, prompt: str, *, purpose: str) -> dict[str, Any]:
        settings = {
            "model": self.model,
            "max_tokens": self.max_tokens,
            "temperature": self.temperature,
            "purpose": purpose,
        }
        prompt_hash = stable_hash(json.dumps(settings, sort_keys=True) + "\n" + prompt)
        if prompt_hash in self.cache:
            cached = dict(self.cache[prompt_hash])
            cached["cache_hit"] = True
            cached["prompt_hash"] = prompt_hash
            return cached

        payload = {
            "model": self.model,
            "messages": [{"role": "user", "content": prompt}],
            "temperature": self.temperature,
            "max_tokens": self.max_tokens,
            "max_completion_tokens": self.max_tokens,
            "response_format": {"type": "json_object"},
        }
        request = urllib.request.Request(
            "https://openrouter.ai/api/v1/chat/completions",
            data=json.dumps(payload).encode("utf-8"),
            headers={
                "Authorization": f"Bearer {self.api_key}",
                "Content-Type": "application/json",
                "HTTP-Referer": "https://localhost/oraclemem",
                "X-Title": "OracleMem Natural Coverage Pilot",
            },
            method="POST",
        )
        try:
            with urllib.request.urlopen(request, timeout=self.timeout) as response:
                body = json.loads(response.read().decode("utf-8"))
        except urllib.error.HTTPError as error:
            details = error.read().decode("utf-8", errors="replace")
            raise RuntimeError(f"OpenRouter HTTP {error.code}: {details}") from error

        content = body["choices"][0]["message"].get("content")
        parsed = extract_json_object(content)
        result = {
            "cache_hit": False,
            "prompt_hash": prompt_hash,
            "purpose": purpose,
            "model": self.model,
            "parsed": parsed,
            "raw_content": content,
            "usage": body.get("usage", {}),
            "provider": body.get("provider"),
        }
        self.cache[prompt_hash] = result
        self._write_cache()
        if self.request_sleep > 0:
            time.sleep(self.request_sleep)
        return result


@dataclass(frozen=True)
class GeneratedSession:
    session_id: str
    date: str
    source_kind: str
    text: str
    response: dict[str, Any]
    prompt_hash: str
    cache_hit: bool
    usage: Mapping[str, Any]


def session_text(turns: Sequence[Mapping[str, Any]], *, max_words: int) -> str:
    lines: list[str] = []
    for turn in turns:
        role = str(turn.get("role", "unknown")).strip() or "unknown"
        content = str(turn.get("content", "")).strip()
        if content:
            lines.append(f"{role}: {content}")
    return truncate_words("\n".join(lines), max_words)


def session_prompt(session_id: str, date: str, text: str) -> str:
    return f"""You are constructing a write-time memory benchmark from one conversation session.



Do not use any hidden question or answer. Use only the session text below.



Extract up to 4 source-backed evidence units that could matter for future long-term memory questions. Then generate alternative candidate memory representations for this same session:

- one Mem0-style atomic fact candidate, if useful;

- one A-Mem-style graph/linked note candidate, if useful;

- one MemGPT-style compact summary candidate, if useful;

- one tombstone/update candidate only if the session explicitly corrects, supersedes, invalidates, or updates prior information.



Every candidate must list which evidence unit ids it supports. Use only ids you created. Do not invent facts unsupported by the session.



Return exactly JSON:

{{

  "evidence_units": [

    {{

      "unit_id": "u1",

      "kind": "current_fact|temporal_fact|preference|update|abstention|other",

      "canonical_text": "...",

      "source_quote": "short exact quote from session",

      "importance": 0.5

    }}

  ],

  "candidates": [

    {{

      "candidate_id": "c1",

      "representation_type": "atomic_fact|graph_edge|summary|tombstone|compound_update",

      "generator": "gemini_mem0|gemini_amem|gemini_memgpt|gemini_validity",

      "text": "...",

      "covers_unit_ids": ["u1"],

      "confidence": 0.8

    }}

  ]

}}



Session id: {session_id}

Session date: {date}

Session text:

{text}

"""


def query_prompt(question: str, answer: str, units: Sequence[Mapping[str, Any]]) -> str:
    payload = [
        {
            "unit_id": row["unit_id"],
            "canonical_text": row["canonical_text"],
            "source_quote": row.get("source_quote", ""),
            "session_id": row.get("session_id", ""),
        }
        for row in units
    ]
    return f"""You are annotating a long-term memory evaluation question.

Select the minimal evidence unit ids needed to answer the question. Use the gold answer only for annotation.
A set of units is sufficient if a careful reader can derive the answer from those units by simple reasoning:
- For temporal questions, include the event/date units needed to compare order or compute a duration.
- For "which happened first/earlier" questions, include units for both compared events when available.
- For update/current-truth questions, include the current-truth unit and any invalidating or superseded unit needed to avoid a stale answer.
- Individual units do not need to literally contain the final answer if their combination supports it.
Return an empty list only when the provided units cannot support the answer even with simple temporal, arithmetic, or update reasoning. Do not create new unit ids.

Return exactly JSON:
{{
  "required_unit_ids": ["..."],

  "rationale": "..."

}}



Question: {question}

Gold answer: {answer}

Evidence units:
{json.dumps(payload, ensure_ascii=False, indent=2)}
"""


def derived_required_units_prompt(
    question: str,
    answer: str,
    sessions: Sequence[GeneratedSession],
    existing_units: Sequence[Mapping[str, Any]],
) -> str:
    session_payload = [
        {
            "session_id": session.session_id,
            "date": session.date,
            "source_kind": session.source_kind,
            "text": truncate_words(session.text, 900),
        }
        for session in sessions
    ]
    unit_payload = [
        {
            "unit_id": row.get("unit_id", ""),
            "canonical_text": row.get("canonical_text", ""),
            "source_quote": row.get("source_quote", ""),
            "session_id": row.get("session_id", ""),
        }
        for row in existing_units
    ]
    payload = {
        "question": question,
        "gold_answer": answer,
        "sessions": session_payload,
        "existing_units": unit_payload,
    }
    return f"""You are adding missing hidden evidence labels for an OracleMem benchmark package.

The memory candidates have already been generated from sessions only. Do not propose or edit memory candidates.
Your task is only to create benchmark evidence units when the existing units are too coarse or omitted the answer-critical fact.

Create the minimal source-backed evidence units needed to answer the question. Use the gold answer only for annotation.
Each unit must be supported by a quote from one of the listed sessions. For temporal questions, create event/date units that allow a reader to compare order or compute the duration; the unit does not have to state the final derived answer.
Return an empty list only if the sessions themselves do not support the answer.

Return exactly JSON:
{{
  "required_evidence_units": [
    {{
      "session_id": "...",
      "canonical_text": "...",
      "source_quote": "...",
      "kind": "temporal_fact|current_fact|update|preference|other",
      "importance": 1.0
    }}
  ],
  "rationale": "..."
}}

PACKAGE:
{json.dumps(payload, ensure_ascii=False, indent=2)}
"""


def clean_float(value: Any, default: float = 0.5) -> float:
    try:
        numeric = float(value)
    except (TypeError, ValueError):
        return default
    if not math.isfinite(numeric):
        return default
    return min(1.0, max(0.0, numeric))


def candidate_cost(representation_type: str, text: str) -> int:
    words = max(1, word_count(text))
    if representation_type == "raw_span":
        return max(12, words)
    if representation_type in {"atomic_fact", "tombstone"}:
        return max(4, min(20, words))
    if representation_type == "graph_edge":
        return max(8, min(35, words))
    if representation_type in {"summary", "compound_update"}:
        return max(10, min(45, words))
    return max(6, min(45, words))


def build_instance(

    example: Mapping[str, Any],

    generated_sessions: Sequence[GeneratedSession],

    query_annotation: Mapping[str, Any],

) -> tuple[OracleMemInstance, dict[str, Any]]:
    question_id = str(example["question_id"])
    candidates: list[CandidateMemory] = []
    unit_rows: list[dict[str, Any]] = []
    unit_weights: dict[str, float] = {}
    current_units: list[str] = []
    invalidation_units: list[str] = []
    stale_units: list[str] = []

    for session_index, generated in enumerate(generated_sessions):
        parsed = generated.response
        local_unit_map: dict[str, str] = {}
        for unit_index, unit in enumerate(parsed.get("evidence_units", []) or []):
            local_id = str(unit.get("unit_id", f"u{unit_index + 1}")).strip()
            global_id = f"{safe_token(question_id)}::{safe_token(generated.session_id)}::{safe_token(local_id)}"
            kind = str(unit.get("kind", "other")).strip() or "other"
            canonical = str(unit.get("canonical_text", "")).strip()
            quote = str(unit.get("source_quote", "")).strip()
            if not canonical:
                continue
            local_unit_map[local_id] = global_id
            importance = clean_float(unit.get("importance"), default=0.5)
            unit_rows.append(
                {
                    "unit_id": global_id,
                    "local_unit_id": local_id,
                    "session_id": generated.session_id,
                    "kind": kind,
                    "canonical_text": canonical,
                    "source_quote": quote,
                    "importance": importance,
                    "source_kind": generated.source_kind,
                    "timestamp": session_index,
                }
            )
            unit_weights.setdefault(global_id, 0.0)
            if kind in {"update", "current_fact", "temporal_fact", "preference"}:
                current_units.append(global_id)
            if kind == "update":
                invalidation_units.append(global_id)

        if local_unit_map:
            raw_coverage = {unit_id: 1.0 for unit_id in local_unit_map.values()}
            raw_text = truncate_words(generated.text, 220)
            candidates.append(
                CandidateMemory(
                    candidate_id=f"{safe_token(question_id)}::{safe_token(generated.session_id)}::raw",
                    experience_id=f"{safe_token(question_id)}::{safe_token(generated.session_id)}",
                    representation_type="raw_span",
                    serialized=raw_text,
                    cost=candidate_cost("raw_span", raw_text),
                    coverage=raw_coverage,
                    time_index=session_index,
                    generator="longmemeval_raw",
                    confidence=1.0,
                )
            )

        for candidate_index, raw_candidate in enumerate(parsed.get("candidates", []) or []):
            text = str(raw_candidate.get("text", "")).strip()
            if not text:
                continue
            representation_type = str(raw_candidate.get("representation_type", "summary")).strip() or "summary"
            if representation_type not in {
                "atomic_fact",
                "graph_edge",
                "summary",
                "tombstone",
                "compound_update",
            }:
                representation_type = "summary"
            coverage: dict[str, float] = {}
            for local_id in raw_candidate.get("covers_unit_ids", []) or []:
                global_id = local_unit_map.get(str(local_id).strip())
                if global_id:
                    coverage[global_id] = 1.0
            if not coverage:
                continue
            generator = str(raw_candidate.get("generator", "gemini_writer")).strip() or "gemini_writer"
            candidates.append(
                CandidateMemory(
                    candidate_id=(
                        f"{safe_token(question_id)}::{safe_token(generated.session_id)}::"
                        f"{safe_token(generator)}_{candidate_index}"
                    ),
                    experience_id=f"{safe_token(question_id)}::{safe_token(generated.session_id)}",
                    representation_type=representation_type,
                    serialized=text,
                    cost=candidate_cost(representation_type, text),
                    coverage=coverage,
                    time_index=session_index,
                    generator=generator,
                    confidence=clean_float(raw_candidate.get("confidence"), default=0.75),
                )
            )

    available_units = {row["unit_id"] for row in unit_rows}
    required_unit_ids = [
        str(unit_id)
        for unit_id in query_annotation.get("required_unit_ids", [])
        if str(unit_id) in available_units
    ]
    for unit_id in required_unit_ids:
        unit_weights[unit_id] = 1.0

    instance = OracleMemInstance(
        instance_id=f"longmemeval_gemini_{safe_token(question_id)}",
        seed=None,
        candidates=tuple(candidates),
        unit_weights=unit_weights,
        current_units=tuple(current_units),
        invalidation_units=tuple(invalidation_units),
        stale_units=tuple(stale_units),
    )
    metadata = {
        "question_id": question_id,
        "question_type": example.get("question_type"),
        "question": example.get("question"),
        "answer": example.get("answer"),
        "answer_session_ids": list(example.get("answer_session_ids", []) or []),
        "required_unit_ids": required_unit_ids,
        "query_annotation": dict(query_annotation),
        "unit_rows": unit_rows,
        "selected_sessions": [asdict(generated) for generated in generated_sessions],
    }
    return instance, metadata


def write_jsonl(path: Path, rows: Iterable[Mapping[str, Any]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8") as handle:
        for row in rows:
            handle.write(json.dumps(dict(row), sort_keys=True) + "\n")


def file_sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def coverage_label(value: float) -> str:
    if value >= 1.0:
        return "full"
    if value >= 0.75:
        return "partial_strong"
    if value >= 0.5:
        return "partial_weak"
    return "hint_only"


def export_natural_package(

    *,

    out_dir: Path,

    instances: Sequence[OracleMemInstance],

    metadata_by_instance: Mapping[str, Mapping[str, Any]],

    model: str,

    cache_path: Path,

    prompt_hashes: Mapping[str, Sequence[str]],

    total_usage: Mapping[str, float],

) -> dict[str, Any]:
    out_dir.mkdir(parents=True, exist_ok=True)
    experience_rows: list[dict[str, Any]] = []
    evidence_rows: list[dict[str, Any]] = []
    query_rows: list[dict[str, Any]] = []
    candidate_rows: list[dict[str, Any]] = []
    coverage_rows: list[dict[str, Any]] = []
    annotation_rows: list[dict[str, Any]] = []

    for instance in instances:
        metadata = dict(metadata_by_instance[instance.instance_id])
        session_meta = {
            row["session_id"]: row for row in metadata.get("selected_sessions", [])
        }
        for session_id, session in sorted(session_meta.items()):
            experience_id = f"{safe_token(metadata['question_id'])}::{safe_token(session_id)}"
            experience_rows.append(
                {
                    "experience_id": experience_id,
                    "session_id": session_id,
                    "timestamp": session.get("date", ""),
                    "text": session.get("text", ""),
                    "split": "longmemeval_s_support_slice",
                    "source_kind": session.get("source_kind", ""),
                    "source_span_ids": [f"{experience_id}:full_session"],
                }
            )
        for unit in metadata.get("unit_rows", []):
            evidence_rows.append(
                {
                    "unit_id": unit["unit_id"],
                    "kind": unit["kind"],
                    "canonical_text": unit["canonical_text"],
                    "source_spans": [
                        {
                            "span_id": f"{safe_token(metadata['question_id'])}::{safe_token(unit['session_id'])}:full_session",
                            "session_id": unit["session_id"],
                            "text": unit.get("source_quote") or unit["canonical_text"],
                        }
                    ],
                    "timestamp": unit.get("timestamp", 0),
                    "state": "current",
                    "proposition_id": unit["unit_id"],
                    "annotator_ids": [model],
                    "adjudication_status": "model_annotated",
                    "unit_weight": float(instance.unit_weights.get(unit["unit_id"], 0.0)),
                    "source_kind": unit.get("source_kind", ""),
                }
            )
        query_rows.append(
            {
                "query_id": metadata["question_id"],
                "question": metadata["question"],
                "answer": metadata["answer"],
                "category": metadata["question_type"],
                "required_unit_ids": metadata.get("required_unit_ids", []),
                "answer_session_ids": metadata.get("answer_session_ids", []),
                "split": "longmemeval_s_support_slice",
                "annotation_rationale": metadata.get("query_annotation", {}).get("rationale", ""),
            }
        )
        for candidate in instance.candidates:
            candidate_rows.append(
                {
                    "candidate_id": candidate.candidate_id,
                    "experience_id": candidate.experience_id,
                    "candidate_group": candidate.experience_id,
                    "representation_type": candidate.representation_type,
                    "text": candidate.serialized,
                    "serialized": candidate.serialized,
                    "cost_tokens": candidate.cost,
                    "cost": candidate.cost,
                    "generator_id": candidate.generator,
                    "confidence": candidate.confidence,
                    "time_index": candidate.time_index,
                }
            )
            for unit_id, value in sorted(candidate.coverage.items()):
                coverage_rows.append(
                    {
                        "candidate_id": candidate.candidate_id,
                        "experience_id": candidate.experience_id,
                        "candidate_group": candidate.experience_id,
                        "unit_id": unit_id,
                        "coverage": float(value),
                        "coverage_label": coverage_label(float(value)),
                        "rationale": "Gemini-generated candidate declares support for this extracted source-backed evidence unit; raw spans cover all units extracted from their source session.",
                        "source_span_ids": [f"{candidate.experience_id}:full_session"],
                        "annotator_ids": [model],
                        "adjudication_status": "model_annotated",
                    }
                )

    for index, row in enumerate(coverage_rows):
        annotation_rows.append(
            {
                "record_id": f"gemini_natural_coverage:{index:06d}",
                "record_type": "coverage_cell",
                "decision": "accepted_model_annotation",
                "primary_annotator": model,
                "verifier": model,
                "adjudicator": "not_human_adjudicated",
                "candidate_id": row["candidate_id"],
                "unit_id": row["unit_id"],
                "notes": "Single-model annotation; not a human-adjudicated final benchmark label.",
            }
        )

    paths = {
        "experiences": out_dir / "experiences.jsonl",
        "evidence_units": out_dir / "evidence_units.jsonl",
        "queries": out_dir / "queries.jsonl",
        "candidate_memories": out_dir / "candidate_memories.jsonl",
        "coverage_matrix": out_dir / "coverage_matrix.jsonl",
        "annotation_decisions": out_dir / "annotation_decisions.jsonl",
    }
    write_jsonl(paths["experiences"], experience_rows)
    write_jsonl(paths["evidence_units"], evidence_rows)
    write_jsonl(paths["queries"], query_rows)
    write_jsonl(paths["candidate_memories"], candidate_rows)
    write_jsonl(paths["coverage_matrix"], coverage_rows)
    write_jsonl(paths["annotation_decisions"], annotation_rows)

    file_hashes = {path.name: file_sha256(path) for path in paths.values()}
    manifest = {
        "schema_version": 1,
        "synthetic_instance": False,
        "dataset": "LongMemEval-S",
        "split": "support-slice pilot",
        "generator_model": model,
        "api_provider": "OpenRouter",
        "api_cache": str(cache_path),
        "prompt_hashes": {key: list(values) for key, values in prompt_hashes.items()},
        "allowed_inputs": [
            "conversation session text for candidate generation",
            "question and gold answer for separate required-unit annotation",
        ],
        "forbidden_inputs_for_candidate_generation": [
            "held-out question text",
            "gold answer",
            "required_unit_ids",
            "solver outputs",
        ],
        "limitations": [
            "support-slice package includes selected answer-support sessions and optional sampled distractors; it is not a full-haystack write-time benchmark",
            "coverage is single-model annotated and not human adjudicated",
            "published-system rows are local policy mappings over Gemini-generated candidate types unless an external system adapter is explicitly reported",
        ],
        "counts": {
            "instances": len(instances),
            "experiences": len(experience_rows),
            "evidence_units": len(evidence_rows),
            "queries": len(query_rows),
            "candidate_memories": len(candidate_rows),
            "positive_coverage_rows": len(coverage_rows),
        },
        "usage": dict(total_usage),
        "file_hashes": file_hashes,
    }
    manifest_path = out_dir / "candidate_generation_manifest.json"
    manifest_path.write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    readme_path = out_dir / "README.md"
    readme_path.write_text(
        "\n".join(
            [
                "# Gemini Natural OracleMem Coverage Package",
                "",
                "This is a LongMemEval-S support-slice pilot, not a finalized human-adjudicated benchmark.",
                "Candidate generation used only conversation sessions. Query/gold answer was used only to annotate required evidence units.",
                "",
                f"Instances: {len(instances)}",
                f"Evidence units: {len(evidence_rows)}",
                f"Candidate memories: {len(candidate_rows)}",
                f"Positive coverage rows: {len(coverage_rows)}",
                "",
            ]
        ),
        encoding="utf-8",
    )
    return {
        "package_dir": str(out_dir),
        "candidate_generation_manifest": str(manifest_path),
        "README": str(readme_path),
        **{key: str(value) for key, value in paths.items()},
    }


def choose_examples(

    examples: Sequence[Mapping[str, Any]],

    *,

    focus_only: bool,

    limit: int,

    seed: int,

) -> list[Mapping[str, Any]]:
    filtered = [
        example
        for example in examples
        if (not focus_only or example.get("question_type") in FOCUS_TYPES)
    ]
    rng = random.Random(seed)
    filtered = sorted(filtered, key=lambda row: str(row.get("question_id", "")))
    rng.shuffle(filtered)
    return filtered[:limit]


def choose_session_indices(example: Mapping[str, Any], *, distractors: int, rng: random.Random) -> list[int]:
    session_ids = list(example.get("haystack_session_ids", []) or [])
    answer_ids = set(example.get("answer_session_ids", []) or [])
    answer_indices = [index for index, sid in enumerate(session_ids) if sid in answer_ids]
    distractor_indices = [index for index, sid in enumerate(session_ids) if sid not in answer_ids]
    rng.shuffle(distractor_indices)
    selected = sorted(set(answer_indices + distractor_indices[:distractors]))
    if not selected and session_ids:
        selected = [len(session_ids) - 1]
    return selected


def usage_totals(api_rows: Sequence[Mapping[str, Any]]) -> dict[str, float]:
    totals = defaultdict(float)
    for row in api_rows:
        usage = row.get("usage", {}) or {}
        for key in ("prompt_tokens", "completion_tokens", "total_tokens", "cost"):
            try:
                totals[key] += float(usage.get(key, 0.0) or 0.0)
            except (TypeError, ValueError):
                pass
        totals["api_calls"] += 0.0 if row.get("cache_hit") else 1.0
        totals["cache_hits"] += 1.0 if row.get("cache_hit") else 0.0
    return dict(totals)


def render_report(

    *,

    summary: Mapping[str, Any],

    resolved_summary: Sequence[Mapping[str, Any]],

    resolved_count: int,

    unresolved_count: int,

    package_paths: Mapping[str, Any],

    audit_summary: Mapping[str, Any] | None,

    usage: Mapping[str, float],

    source_repos: Mapping[str, str],

) -> str:
    lines = [
        "# Gemini Natural OracleMem Pilot",
        "",
        "This run uses Gemini through OpenRouter to build a LongMemEval-S support-slice coverage package.",
        "It is stronger than synthetic-only evidence, but it is not yet a full non-synthetic benchmark because labels are single-model annotated and the haystack is sliced to selected support/distractor sessions.",
        "",
        "## Source Repos Inspected",
        "",
    ]
    for name, path in sorted(source_repos.items()):
        lines.append(f"- `{name}`: `{path}`")
    lines.extend(
        [
            "",
            "## API Usage",
            "",
            f"- New API calls: {int(usage.get('api_calls', 0.0))}",
            f"- Cache hits: {int(usage.get('cache_hits', 0.0))}",
            f"- Total tokens: {usage.get('total_tokens', 0.0):.0f}",
            f"- Estimated cost from OpenRouter usage: ${usage.get('cost', 0.0):.4f}",
            f"- Coverage-resolved instances: {resolved_count}",
            f"- Unresolved instances with zero required units: {unresolved_count}",
            "",
            "## Coverage Package",
            "",
        ]
    )
    if int(usage.get("api_calls", 0.0)) == 0 and int(usage.get("cache_hits", 0.0)) > 0 and usage.get("total_tokens", 0.0) > 0:
        lines[-2:-2] = [
            "Note: this report was regenerated from cache. The cached rerun made zero additional API calls while preserving historical token/cost metadata from the original uncached calls.",
            "",
        ]
    for key, value in sorted(package_paths.items()):
        lines.append(f"- `{key}`: `{value}`")
    if audit_summary:
        ready = audit_summary.get("coverage_ready_artifacts", [])
        lines.extend(
            [
                "",
                "## Structural Audit",
                "",
                f"- Coverage-ready artifacts according to structural audit: {ready}",
            ]
        )
    lines.extend(["", "## Aggregate Results", ""])
    for row in summary.get("by_budget_method", []):
        lines.append(
            "- budget {budget}, `{method}`: ratio_to_opt={ratio:.3f}, objective={obj:.3f}, cost={cost:.1f}, feasible={feasible}".format(
                budget=row.get("budget"),
                method=row.get("method"),
                ratio=row.get("mean_ratio_to_opt", 0.0),
                obj=row.get("mean_objective", 0.0),
                cost=row.get("mean_selected_cost", 0.0),
                feasible=row.get("all_budget_feasible") and row.get("all_group_feasible"),
            )
        )
    lines.extend(
        [
            "",
            "## Coverage-Resolved Subset",
            "",
            "These rows exclude examples whose required evidence units could not be resolved from the generated coverage package. This is the safer number for paper discussion.",
            "",
        ]
    )
    for row in resolved_summary:
        lines.append(
            "- budget {budget}, `{method}`: n={n}, ratio_to_opt={ratio:.3f}, objective={obj:.3f}, cost={cost:.1f}".format(
                budget=row["budget"],
                method=row["method"],
                n=row["n"],
                ratio=row["mean_ratio_to_opt"],
                obj=row["mean_objective"],
                cost=row["mean_selected_cost"],
            )
        )
    lines.extend(
        [
            "",
            "## Interpretation Boundary",
            "",
            "- Candidate generation is query-independent at the session level.",
            "- Required-unit annotation uses the question and gold answer; this is benchmark labeling, not a writer input.",
            "- The MemGPT/Mem0/A-Mem/A-MAC rows use local policy mappings over Gemini-generated candidate types. They are not full published-system executions unless a future adapter records that explicitly.",
            "- This pilot is suitable as a NeurIPS rebuttal/progress artifact, not as the final main empirical table without scaling and adjudication.",
            "",
        ]
    )
    return "\n".join(lines)


def aggregate_resolved_subset(
    results: Sequence[SelectionResult],
    metadata_by_instance: Mapping[str, Mapping[str, Any]],
) -> list[dict[str, Any]]:
    grouped: dict[tuple[int, str], list[SelectionResult]] = defaultdict(list)
    for row in results:
        metadata = metadata_by_instance.get(row.instance_id, {})
        if not metadata.get("required_unit_ids"):
            continue
        grouped[(int(row.budget), str(row.method))].append(row)
    summary: list[dict[str, Any]] = []
    for (budget, method), rows in sorted(grouped.items()):
        ratios = [float(row.ratio_to_opt) for row in rows if row.ratio_to_opt is not None]
        summary.append(
            {
                "budget": budget,
                "method": method,
                "n": len(rows),
                "mean_ratio_to_opt": statistics.mean(ratios) if ratios else 0.0,
                "mean_objective": statistics.mean(float(row.objective_value) for row in rows),
                "mean_selected_cost": statistics.mean(float(row.selected_cost) for row in rows),
                "all_budget_feasible": all(row.budget_feasible for row in rows),
                "all_group_feasible": all(row.group_feasible for row in rows),
            }
        )
    return summary


def resolution_rows(metadata_by_instance: Mapping[str, Mapping[str, Any]]) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
    """Return resolved/unresolved example rows for downstream natural-package runs."""

    resolved: list[dict[str, Any]] = []
    unresolved: list[dict[str, Any]] = []
    for instance_id, metadata in sorted(metadata_by_instance.items()):
        row = {
            "instance_id": instance_id,
            "question_id": metadata.get("question_id"),
            "question_type": metadata.get("question_type"),
            "question": metadata.get("question"),
            "answer": metadata.get("answer"),
            "answer_session_ids": metadata.get("answer_session_ids", []),
            "required_unit_ids": metadata.get("required_unit_ids", []),
            "selected_session_ids": [
                session.get("session_id")
                for session in metadata.get("selected_sessions", [])
            ],
            "n_units": len(metadata.get("unit_rows", [])),
            "n_required_units": len(metadata.get("required_unit_ids", [])),
        }
        if row["required_unit_ids"]:
            resolved.append(row)
        else:
            row["unresolved_reason"] = "no_required_units_resolved_from_generated_evidence"
            unresolved.append(row)
    return resolved, unresolved


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--dataset-json", type=Path, default=Path("llm_memory_validation/cache/longmemeval_s_cleaned.json"))
    parser.add_argument("--out-dir", type=Path, default=Path("llm_memory_validation/gemini_natural_oraclemem_pilot"))
    parser.add_argument("--api-env", type=Path, default=Path("api.env"))
    parser.add_argument("--api-cache", type=Path, default=None)
    parser.add_argument("--model", default=DEFAULT_MODEL)
    parser.add_argument("--limit", type=int, default=8)
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--distractors-per-example", type=int, default=2)
    parser.add_argument("--max-session-words", type=int, default=850)
    parser.add_argument("--budgets", default="30,60")
    parser.add_argument("--methods", default=",".join(DEFAULT_METHODS))
    parser.add_argument("--focus-only", action="store_true", default=True)
    parser.add_argument("--no-focus-only", action="store_false", dest="focus_only")
    parser.add_argument("--max-tokens", type=int, default=1400)
    parser.add_argument("--request-sleep", type=float, default=0.02)
    args = parser.parse_args()

    env = load_env_file(args.api_env)
    api_key = env.get("OPENROUTER_API_KEY")
    if not api_key:
        raise RuntimeError(f"OPENROUTER_API_KEY not found in {args.api_env}")

    args.out_dir.mkdir(parents=True, exist_ok=True)
    api_cache = args.api_cache or (args.out_dir / "openrouter_cache_gemini_natural_oraclemem.json")
    client = OpenRouterJsonClient(
        api_key=api_key,
        model=args.model,
        cache_path=api_cache,
        max_tokens=args.max_tokens,
        request_sleep=args.request_sleep,
    )

    examples = json.loads(args.dataset_json.read_text(encoding="utf-8"))
    selected_examples = choose_examples(
        examples,
        focus_only=args.focus_only,
        limit=args.limit,
        seed=args.seed,
    )
    rng = random.Random(args.seed)
    instances: list[OracleMemInstance] = []
    metadata_by_instance: dict[str, dict[str, Any]] = {}
    api_rows: list[dict[str, Any]] = []
    prompt_hashes: dict[str, list[str]] = defaultdict(list)

    for example_index, example in enumerate(selected_examples):
        session_ids = list(example.get("haystack_session_ids", []) or [])
        session_dates = list(example.get("haystack_dates", []) or [])
        sessions = list(example.get("haystack_sessions", []) or [])
        answer_ids = set(example.get("answer_session_ids", []) or [])
        generated_sessions: list[GeneratedSession] = []
        for session_index in choose_session_indices(
            example,
            distractors=args.distractors_per_example,
            rng=rng,
        ):
            if session_index >= len(sessions):
                continue
            sid = str(session_ids[session_index]) if session_index < len(session_ids) else f"session_{session_index}"
            date = str(session_dates[session_index]) if session_index < len(session_dates) else ""
            text = session_text(sessions[session_index], max_words=args.max_session_words)
            source_kind = "answer_support" if sid in answer_ids else "distractor"
            response = client(
                session_prompt(sid, date, text),
                purpose="session_candidate_generation",
            )
            api_rows.append(response)
            prompt_hashes["session_candidate_generation"].append(str(response["prompt_hash"]))
            generated_sessions.append(
                GeneratedSession(
                    session_id=sid,
                    date=date,
                    source_kind=source_kind,
                    text=text,
                    response=dict(response.get("parsed", {})),
                    prompt_hash=str(response["prompt_hash"]),
                    cache_hit=bool(response.get("cache_hit")),
                    usage=dict(response.get("usage", {}) or {}),
                )
            )

        all_unit_rows: list[dict[str, Any]] = []
        for generated in generated_sessions:
            for unit in generated.response.get("evidence_units", []) or []:
                local_id = str(unit.get("unit_id", "")).strip()
                if not local_id:
                    continue
                global_id = f"{safe_token(example['question_id'])}::{safe_token(generated.session_id)}::{safe_token(local_id)}"
                all_unit_rows.append(
                    {
                        "unit_id": global_id,
                        "canonical_text": str(unit.get("canonical_text", "")).strip(),
                        "source_quote": str(unit.get("source_quote", "")).strip(),
                        "session_id": generated.session_id,
                    }
                )
        query_response = client(
            query_prompt(
                str(example.get("question", "")),
                str(example.get("answer", "")),
                all_unit_rows,
            ),
            purpose="query_required_unit_annotation",
        )
        api_rows.append(query_response)
        prompt_hashes["query_required_unit_annotation"].append(str(query_response["prompt_hash"]))
        query_annotation = dict(query_response.get("parsed", {}))
        available_unit_ids = {str(row["unit_id"]) for row in all_unit_rows}
        resolved_required_ids = [
            str(unit_id)
            for unit_id in query_annotation.get("required_unit_ids", []) or []
            if str(unit_id) in available_unit_ids
        ]
        if not resolved_required_ids and generated_sessions:
            derived_response = client(
                derived_required_units_prompt(
                    str(example.get("question", "")),
                    str(example.get("answer", "")),
                    generated_sessions,
                    all_unit_rows,
                ),
                purpose="query_derived_required_unit_annotation",
            )
            api_rows.append(derived_response)
            prompt_hashes["query_derived_required_unit_annotation"].append(str(derived_response["prompt_hash"]))
            derived = derived_response.get("parsed", {}) if isinstance(derived_response, Mapping) else {}
            session_by_id = {session.session_id: session for session in generated_sessions}
            derived_required_ids: list[str] = []
            local_counts: dict[str, int] = defaultdict(int)
            for session in generated_sessions:
                local_counts[session.session_id] = len(session.response.get("evidence_units", []) or [])
            for unit in derived.get("required_evidence_units", []) or []:
                if not isinstance(unit, Mapping):
                    continue
                session_id = str(unit.get("session_id", "")).strip()
                if session_id not in session_by_id:
                    continue
                canonical = str(unit.get("canonical_text", "")).strip()
                quote = str(unit.get("source_quote", "")).strip()
                if not canonical:
                    continue
                local_counts[session_id] += 1
                local_id = f"dq{local_counts[session_id]}"
                session = session_by_id[session_id]
                session.response.setdefault("evidence_units", []).append(
                    {
                        "unit_id": local_id,
                        "canonical_text": canonical,
                        "source_quote": quote,
                        "kind": str(unit.get("kind", "temporal_fact")).strip() or "temporal_fact",
                        "importance": clean_float(unit.get("importance"), default=1.0),
                    }
                )
                global_id = f"{safe_token(example['question_id'])}::{safe_token(session_id)}::{safe_token(local_id)}"
                derived_required_ids.append(global_id)
            if derived_required_ids:
                query_annotation = {
                    "required_unit_ids": derived_required_ids,
                    "rationale": (
                        "Derived evidence-unit fallback: "
                        + str(derived.get("rationale", query_annotation.get("rationale", "")))
                    ),
                    "derived_required_unit_annotation": True,
                    "initial_query_annotation": dict(query_response.get("parsed", {})),
                }
        instance, metadata = build_instance(
            example,
            generated_sessions,
            query_annotation,
        )
        if not instance.candidates:
            continue
        instances.append(instance)
        metadata_by_instance[instance.instance_id] = metadata
        print(
            f"[{example_index + 1}/{len(selected_examples)}] {example.get('question_id')} "
            f"candidates={len(instance.candidates)} required={len(metadata['required_unit_ids'])}"
        )

    budgets = [int(part.strip()) for part in args.budgets.split(",") if part.strip()]
    methods = [part.strip() for part in args.methods.split(",") if part.strip()]
    results: list[SelectionResult] = []
    for instance in instances:
        results.extend(
            evaluate_instance(
                instance,
                budgets,
                methods=methods,
                retrieval_modes=("fixed", "oracle"),
            )
        )

    paths = write_benchmark_outputs(results, args.out_dir)
    usage = usage_totals(api_rows)
    package_paths = export_natural_package(
        out_dir=args.out_dir / "coverage_package",
        instances=instances,
        metadata_by_instance=metadata_by_instance,
        model=args.model,
        cache_path=api_cache,
        prompt_hashes=prompt_hashes,
        total_usage=usage,
    )
    api_rows_path = args.out_dir / "api_calls.jsonl"
    write_jsonl(api_rows_path, api_rows)
    metadata_path = args.out_dir / "instance_metadata.json"
    metadata_path.write_text(json.dumps(metadata_by_instance, indent=2, sort_keys=True), encoding="utf-8")

    audit_summary = None
    audit_path = args.out_dir / "coverage_audit" / "summary.json"
    if audit_path.exists():
        audit_summary = json.loads(audit_path.read_text(encoding="utf-8"))

    summary = json.loads(Path(paths["summary_json"]).read_text(encoding="utf-8"))
    resolved_count = sum(1 for metadata in metadata_by_instance.values() if metadata.get("required_unit_ids"))
    unresolved_count = len(metadata_by_instance) - resolved_count
    resolved_summary = aggregate_resolved_subset(results, metadata_by_instance)
    resolved_summary_path = args.out_dir / "coverage_resolved_summary.json"
    resolved_rows, unresolved_rows = resolution_rows(metadata_by_instance)
    resolved_rows_path = args.out_dir / "resolved_examples.jsonl"
    unresolved_rows_path = args.out_dir / "unresolved_examples.jsonl"
    write_jsonl(resolved_rows_path, resolved_rows)
    write_jsonl(unresolved_rows_path, unresolved_rows)
    resolution_report_path = args.out_dir / "coverage_resolution_report.md"
    resolution_rate = (len(resolved_rows) / len(metadata_by_instance)) if metadata_by_instance else 0.0
    resolution_report_path.write_text(
        "\n".join(
            [
                "# Coverage Resolution Report",
                "",
                f"- Attempted/constructed instances: {len(metadata_by_instance)}",
                f"- Coverage-resolved instances: {len(resolved_rows)}",
                f"- Unresolved instances: {len(unresolved_rows)}",
                f"- Coverage-resolved rate: {resolution_rate:.3f}",
                "",
                "An instance is coverage-resolved when the query annotation maps at least one required evidence unit to evidence units generated from the selected support/distractor sessions or the source-backed derived-unit annotation pass.",
            ]
        )
        + "\n",
        encoding="utf-8",
    )
    resolved_summary_path.write_text(
        json.dumps(
            {
                "coverage_resolved_instances": resolved_count,
                "unresolved_instances": unresolved_count,
                "by_budget_method": resolved_summary,
            },
            indent=2,
            sort_keys=True,
        )
        + "\n",
        encoding="utf-8",
    )
    report = render_report(
        summary=summary,
        resolved_summary=resolved_summary,
        resolved_count=resolved_count,
        unresolved_count=unresolved_count,
        package_paths=package_paths,
        audit_summary=audit_summary,
        usage=usage,
        source_repos={
            "Mem0": "external_repos/mem0",
            "A-Mem": "external_repos/AgenticMemory",
            "Letta/MemGPT": "external_repos/letta",
        },
    )
    report_path = args.out_dir / "REPORT.md"
    report_path.write_text(report, encoding="utf-8")

    run_manifest = {
        "schema_version": 1,
        "model": args.model,
        "limit": args.limit,
        "focus_only": args.focus_only,
        "distractors_per_example": args.distractors_per_example,
        "instances": len(instances),
        "budgets": budgets,
        "methods": methods,
        "paths": {
            **paths,
            "package": package_paths,
            "api_calls": str(api_rows_path),
            "metadata": str(metadata_path),
            "coverage_resolved_summary": str(resolved_summary_path),
            "resolved_examples": str(resolved_rows_path),
            "unresolved_examples": str(unresolved_rows_path),
            "coverage_resolution_report": str(resolution_report_path),
            "report": str(report_path),
        },
        "usage": usage,
    }
    (args.out_dir / "run_manifest.json").write_text(
        json.dumps(run_manifest, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )
    print(json.dumps(run_manifest, indent=2, sort_keys=True))


if __name__ == "__main__":
    main()