File size: 25,305 Bytes
465b477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Benchmark generation, human review and freezing.

Candidate questions are generated deterministically from the provenance graph with
fixed templates. Gold answers are *defined by source chunks*: every gold edge carries a
verbatim quote, and items are only released after a human checks the question, the
gold answer and the cited evidence against the source text (``graphtrace review``).
Generated items start as ``unreviewed``; ``freeze`` exports accepted/edited items only.

Threat to validity (see docs/PROTOCOL.md): questions are derived from the same graph
that graph-based methods retrieve from. Mitigations: gold is checked against source
text by a human, text-RAG sees the same source chunks, and extraction errors that slip
into gold are caught at review time (rejected items are reported).
"""

from __future__ import annotations

import hashlib
import json
import random
import re
from collections import defaultdict
from datetime import UTC, datetime
from pathlib import Path

from pydantic import BaseModel

from .canonical import mention_pattern, normalise_text
from .graph import KG, value_groups
from .llm import LLMClient, LLMError, call_structured
from .prompts import PARAPHRASE_SYSTEM, ParaphraseOut
from .schemas import BenchItem, Chunk, Edge, PathStep, Review


class BenchConfig(BaseModel):
    seed: int = 13
    targets: dict[str, int] = {"single_hop": 70, "multi_hop": 70, "false_premise": 40, "conflict_or_insufficient": 40}
    max_answer_set: int = 5
    min_edge_confidence: float | None = None
    popular_min_papers: int = 2  # an entity used for false premises must appear in >= this many papers


def _iid(*parts: object) -> str:
    return hashlib.sha1("|".join(map(str, parts)).encode("utf-8")).hexdigest()[:10]


def _step(e: Edge) -> PathStep:
    return PathStep(edge_id=e.edge_id, subject=e.subject, relation=e.relation, object=e.object)


class Generator:
    def __init__(self, kg: KG, chunks_by_doc: dict[str, list[Chunk]], cfg: BenchConfig):
        # Gold is computed on the same confidence-filtered graph that methods retrieve from
        # (KG.filtered), so both always see the same edge set.
        self.kg = kg.filtered(cfg.min_edge_confidence)
        self.cfg = cfg
        self.chunks_by_doc = chunks_by_doc
        self.chunk_text = {c.chunk_id: normalise_text(c.text) for cs in chunks_by_doc.values() for c in cs}
        self.doc_text = {d: " ".join(self.chunk_text[c.chunk_id] for c in cs) for d, cs in chunks_by_doc.items()}
        self.papers = sorted(n.node_id for n in self.kg.nodes.values() if n.type == "Paper")
        self.chunk_doc = {c.chunk_id: c.doc_id for cs in chunks_by_doc.values() for c in cs}

    # ---- helpers ----

    def objs(self, s: str, rel: str) -> dict[str, list[Edge]]:
        out: dict[str, list[Edge]] = defaultdict(list)
        for e in self.kg.out_edges(s, rel):
            out[e.object].append(e)
        return out

    def subs(self, o: str, rel: str) -> dict[str, list[Edge]]:
        out: dict[str, list[Edge]] = defaultdict(list)
        for e in self.kg.in_edges(o, rel):
            out[e.subject].append(e)
        return out

    def paper_datasets(self, pid: str) -> dict[str, list[Edge]]:
        """Datasets a paper evaluates on, directly or through a method it proposes.

        Assumption (stated in item notes): a paper's evaluation datasets and its proposed
        method's evaluation datasets are interchangeable. Method-derived entries carry the
        method's evaluates_on edge first and the proposes edge as provenance.
        """
        out: dict[str, list[Edge]] = defaultdict(list)
        for d, es in self.objs(pid, "evaluates_on").items():
            out[d].extend(es)
        for m, pe in self.objs(pid, "proposes").items():
            for d, es in self.objs(m, "evaluates_on").items():
                out[d].extend(es + pe)
        return out

    def forms(self, node_id: str) -> list[re.Pattern]:
        node = self.kg.nodes[node_id]
        pats = (mention_pattern(f) for f in {node.name, *node.aliases, node_id.split(":", 1)[1]})
        return [p for p in pats if p is not None]

    def title(self, pid: str) -> str:
        return self.kg.name(pid)

    def mentioned_in_paper(self, node_id: str, doc_id: str) -> bool:
        text = self.doc_text.get(doc_id, "")
        return any(p.search(text) for p in self.forms(node_id))

    def papers_of(self, node_id: str) -> set[str]:
        return {self.kg.nodes[e.subject].doc_id for e in self.kg.in_edges(node_id) if self.kg.nodes[e.subject].type == "Paper"}

    def item(
        self,
        category: str,
        template_id: str,
        hops: int,
        question: str,
        decision: str,
        answer_ids: list[str],
        edges: list[Edge],
        notes: str,
        answer_names: list[str] | None = None,
        extra_chunks: list[str] | None = None,
    ) -> BenchItem:
        names = answer_names if answer_names is not None else [self.kg.name(a) for a in answer_ids]
        chunks = sorted({e.source_chunk_id for e in edges} | set(extra_chunks or []))
        return BenchItem(
            item_id=f"{category[:2]}_{template_id}_{_iid(template_id, question)}",
            category=category,  # type: ignore[arg-type]
            difficulty=hops,
            template_id=template_id,
            question=question,
            question_template=question,
            gold_decision=decision,  # type: ignore[arg-type]
            gold_answer=names,
            gold_answer_ids=answer_ids,
            required_path=[_step(e) for e in edges],
            gold_chunk_ids=chunks,
            annotation_notes=notes,
        )

    @staticmethod
    def _first(es: list[Edge]) -> Edge:
        return min(es, key=lambda e: e.edge_id)

    def _set_item(self, cat, tid, hops, q, answer: dict[str, list[Edge]], extra_edges: list[Edge], notes) -> BenchItem | None:
        if not (1 <= len(answer) <= self.cfg.max_answer_set):
            return None
        ids = sorted(answer)
        edges = extra_edges + [self._first(answer[a]) for a in ids]
        prov = sorted({e.source_chunk_id for a in ids for e in answer[a]})
        return self.item(cat, tid, hops, q, "answer", ids, edges, notes, extra_chunks=prov)

    # ---- single hop ----

    def single_hop(self) -> list[BenchItem]:
        items = []
        spec = [
            ("S1", "evaluates_on", 'Which datasets does the paper "{t}" evaluate on?'),
            ("S2", "proposes", 'Which method does the paper "{t}" propose?'),
            ("S3", "addresses", 'Which task does the paper "{t}" address?'),
            ("S4", "reports_metric", 'Which evaluation metrics does the paper "{t}" report?'),
        ]
        for pid in self.papers:
            for tid, rel, tpl in spec:
                it = self._set_item(
                    "single_hop", tid, 1, tpl.format(t=self.title(pid)), self.objs(pid, rel), [], f"Gold = all '{rel}' objects of the paper in the graph."
                )
                if it:
                    items.append(it)
        # S5: unique result value lookup (same surface form as the conflict template C1)
        groups = defaultdict(list)
        for r in self.kg.results.values():
            groups[(r.method, r.dataset, r.metric)].append(r)
        for (m, d, k), rs in sorted(groups.items()):
            if all(r.value_num is not None for r in rs) and len(value_groups([r.value for r in rs])) == 1:
                q = f"What {self.kg.name(k)} does {self.kg.name(m)} achieve on {self.kg.name(d)}?"
                items.append(
                    self.item(
                        "single_hop",
                        "S5",
                        1,
                        q,
                        "answer",
                        [rs[0].value],
                        [],
                        "Gold = the reported value; all result records agree up to scale and rounding.",
                        answer_names=[rs[0].value],
                        extra_chunks=[r.source_chunk_id for r in rs],
                    ).model_copy(update={"required_path": [PathStep(edge_id=r.result_id, subject=m, relation=f"result:{k}", object=d) for r in rs]})
                )
        return items

    # ---- multi hop ----

    def multi_hop(self) -> list[BenchItem]:
        items = []
        pd = {p: self.paper_datasets(p) for p in self.papers}
        for pid in self.papers:
            t = self.title(pid)
            proposed = self.objs(pid, "proposes")
            # M1: P -proposes-> M -evaluates_on-> D
            if len(proposed) == 1:
                m, pe = next(iter(proposed.items()))
                it = self._set_item(
                    "multi_hop",
                    "M1",
                    2,
                    f'On which datasets is the method proposed in "{t}" evaluated?',
                    pd[pid],
                    [self._first(pe)],
                    "Path: paper proposes method; method evaluates_on dataset. Gold = union of the method's evaluates_on "
                    "datasets and the proposing paper's evaluates_on datasets (assumption: the paper's experiments evaluate "
                    "the method it proposes). REVIEWER: remove datasets used only for other purposes.",
                )
                if it:
                    items.append(it)
                # M4: P -proposes-> M -compares_with-> M2 <-proposes- P2
                ans: dict[str, list[Edge]] = defaultdict(list)
                hop2: list[Edge] = []
                for m2, ce in self.objs(m, "compares_with").items():
                    for p2, pe2 in self.subs(m2, "proposes").items():
                        if p2 != pid:
                            ans[p2].extend(pe2)
                            hop2.append(self._first(ce))
                it = self._set_item(
                    "multi_hop",
                    "M4",
                    3,
                    f'Which paper in the corpus proposes a method that the method proposed in "{t}" is compared with?',
                    ans,
                    [self._first(pe)] + sorted({e.edge_id: e for e in hop2}.values(), key=lambda e: e.edge_id),
                    "Path: paper proposes M; M compares_with M2; other paper proposes M2.",
                )
                if it:
                    items.append(it)
            # M2: P -evaluates_on-> D <-evaluates_on- P2
            ans2: dict[str, list[Edge]] = defaultdict(list)
            bridge: dict[str, Edge] = {}
            for d, de in pd[pid].items():
                for p2 in self.papers:
                    if p2 != pid and d in pd[p2]:
                        ans2[p2].extend(pd[p2][d])
                        bridge[d] = self._first(de)
            it = self._set_item(
                "multi_hop",
                "M2",
                2,
                f'Which other papers in the corpus evaluate on at least one of the same datasets as "{t}"?',
                ans2,
                sorted(bridge.values(), key=lambda e: e.edge_id),
                "Path: paper evaluates_on D; another paper evaluates_on D. A paper counts as evaluating on D directly or through a method it proposes.",
            )
            if it:
                items.append(it.model_copy(update={"gold_answer": [self.title(a) for a in it.gold_answer_ids]}))
        # M3: M <-uses- P -evaluates_on-> D  (aggregated over papers using M)
        for m in sorted(n.node_id for n in self.kg.nodes.values() if n.type == "Method"):
            users: dict[str, list[Edge]] = defaultdict(list)
            for rel in ("uses", "proposes"):
                for p, es in self.subs(m, rel).items():
                    if self.kg.nodes[p].type == "Paper":
                        users[p].extend(es)
            if len(users) < 2:
                continue
            ans3: dict[str, list[Edge]] = defaultdict(list)
            hop1 = []
            for p, ue in users.items():
                ds = pd[p]
                if ds:
                    hop1.append(self._first(ue))
                for d, de in ds.items():
                    ans3[d].extend(de)
            it = self._set_item(
                "multi_hop",
                "M3",
                2,
                f"Which datasets are evaluated on by papers in the corpus that use {self.kg.name(m)}?",
                ans3,
                sorted(hop1, key=lambda e: e.edge_id),
                "Path: paper uses or proposes M; that paper evaluates_on D, directly or through its proposed method (union over papers).",
            )
            if it:
                items.append(it)
        return items

    # ---- false premise ----

    def false_premise(self) -> list[BenchItem]:
        items = []
        popular_d = [n.node_id for n in self.kg.nodes.values() if n.type == "Dataset" and len(self.papers_of(n.node_id)) >= self.cfg.popular_min_papers]
        popular_m = [n.node_id for n in self.kg.nodes.values() if n.type == "Method" and len(self.papers_of(n.node_id)) >= self.cfg.popular_min_papers]
        rng = random.Random(self.cfg.seed)
        for pid in self.papers:
            doc = self.kg.nodes[pid].doc_id
            t = self.title(pid)
            proposed = self.objs(pid, "proposes")
            if len(proposed) == 1:
                _, pe = next(iter(proposed.items()))
                cands = [d for d in sorted(popular_d) if d not in self.paper_datasets(pid) and not self.mentioned_in_paper(d, doc)]
                if cands:
                    d = rng.choice(cands)
                    items.append(
                        self.item(
                            "false_premise",
                            "F1",
                            2,
                            f'What results does the method proposed in "{t}" achieve on {self.kg.name(d)}?',
                            "abstain",
                            [],
                            [self._first(pe)],
                            f"False premise: no evaluates_on edge from the paper or its method to {self.kg.name(d)}, and the dataset is not mentioned anywhere in the paper's text.",
                        )
                    )
            evals = self.objs(pid, "evaluates_on")
            if evals:
                used = set(self.objs(pid, "uses")) | set(proposed)
                cands = [m for m in sorted(popular_m) if m not in used and not self.mentioned_in_paper(m, doc)]
                if cands:
                    m2 = rng.choice(cands)
                    items.append(
                        self.item(
                            "false_premise",
                            "F2",
                            2,
                            f'Which datasets did the paper "{t}" use to evaluate {self.kg.name(m2)}?',
                            "abstain",
                            [],
                            [self._first(min(evals.values(), key=lambda es: es[0].edge_id))],
                            f"False premise: the paper neither proposes nor uses {self.kg.name(m2)} and never mentions it.",
                        )
                    )
        return items

    # ---- conflict / insufficient ----

    def conflict_or_insufficient(self) -> list[BenchItem]:
        items = []
        groups = defaultdict(list)
        for r in self.kg.results.values():
            groups[(r.method, r.dataset, r.metric)].append(r)
        for (m, d, k), rs in sorted(groups.items()):
            numeric = [r for r in rs if r.value_num is not None]
            vgroups = value_groups(sorted({r.value for r in numeric}))
            chunks = {r.source_chunk_id for r in numeric}
            if len(vgroups) >= 2 and len(chunks) >= 2:
                q = f"What {self.kg.name(k)} does {self.kg.name(m)} achieve on {self.kg.name(d)}?"
                values = sorted({r.value for r in numeric})
                docs = {self.chunk_doc.get(r.source_chunk_id) for r in numeric}
                scope = (
                    "Sources disagree across papers: the values come from different papers (for example a paper's own "
                    "number and another paper's reproduction or citation). This counts as a disagreement. "
                    if len(docs) > 1
                    else "The values come from different passages of the same paper. "
                )
                items.append(
                    self.item(
                        "conflict_or_insufficient",
                        "C1",
                        1,
                        q,
                        "flag_conflict",
                        values,
                        [],
                        "Conflict: values that differ after scale normalisation and rounding to the reported precision, for "
                        "the same (method, dataset, metric). " + scope + "REVIEWER: reject if the values refer to different "
                        "settings (data split, model size, metric variant).",
                        answer_names=values,
                        extra_chunks=sorted(chunks),
                    ).model_copy(update={"required_path": [PathStep(edge_id=r.result_id, subject=m, relation=f"result:{k}", object=d) for r in numeric]})
                )
        # C2: method evaluated on dataset, but no reported value anywhere in the corpus
        for e in sorted(self.kg.edges.values(), key=lambda e: e.edge_id):
            if e.relation != "evaluates_on" or self.kg.nodes[e.subject].type != "Method":
                continue
            m, d = e.subject, e.object
            if self.kg.results_for(method=m, dataset=d):
                continue
            mf, df = self.forms(m), self.forms(d)
            # A passage naming both (any alias, same normalisation on both sides) next to any
            # number, integer or decimal, might report a value: not safely "insufficient".
            if any(any(p.search(t) for p in mf) and any(p.search(t) for p in df) and re.search(r"\d", t) for t in self.chunk_text.values()):
                continue
            doc = self.chunk_doc.get(e.source_chunk_id)
            paper_metrics = self.objs(f"paper:{doc}", "reports_metric") if doc else {}
            if not paper_metrics:
                continue  # ask only about a metric the source paper actually reports
            k = min(paper_metrics)
            items.append(
                self.item(
                    "conflict_or_insufficient",
                    "C2",
                    1,
                    f"What {self.kg.name(k)} does {self.kg.name(m)} achieve on {self.kg.name(d)}?",
                    "abstain",
                    [],
                    [e],
                    "Insufficient evidence: the method is evaluated on the dataset, but no passage in the corpus states a value for this pair. "
                    "REVIEWER: confirm no table/figure caption gives the number.",
                )
            )
        return items

    # ---- sampling ----

    def generate(self) -> tuple[list[BenchItem], dict]:
        pools = {
            "single_hop": self.single_hop(),
            "multi_hop": self.multi_hop(),
            "false_premise": self.false_premise(),
            "conflict_or_insufficient": self.conflict_or_insufficient(),
        }
        rng = random.Random(self.cfg.seed)
        chosen: list[BenchItem] = []
        stats: dict = {"available": {}, "selected": {}, "by_template": {}}
        for cat, items in pools.items():
            uniq = {it.item_id: it for it in items}
            by_t: dict[str, list[BenchItem]] = defaultdict(list)
            for it in sorted(uniq.values(), key=lambda i: i.item_id):
                by_t[it.template_id].append(it)
            for lst in by_t.values():
                rng.shuffle(lst)
            stats["available"][cat] = {t: len(v) for t, v in sorted(by_t.items())}
            # Round-robin across templates so no single template dominates a category.
            picked: list[BenchItem] = []
            target = self.cfg.targets.get(cat, 0)
            order = sorted(by_t)
            while len(picked) < target and any(by_t[t] for t in order):
                for t in order:
                    if by_t[t] and len(picked) < target:
                        picked.append(by_t[t].pop())
            stats["selected"][cat] = len(picked)
            for it in picked:
                stats["by_template"][it.template_id] = stats["by_template"].get(it.template_id, 0) + 1
            chosen.extend(picked)
        return sorted(chosen, key=lambda i: i.item_id), stats


# ---- paraphrase (optional, LLM) --------------------------------------------------


def protected_spans(item: BenchItem) -> list[str]:
    spans = re.findall(r'"([^"]+)"', item.question_template)
    return spans


def paraphrase(client: LLMClient, item: BenchItem, required_names: list[str]) -> tuple[str, str | None]:
    """Return (question, rejection reason). Falls back to the template on any problem."""
    try:
        out = call_structured(client, None, "paraphrase", PARAPHRASE_SYSTEM, item.question_template, ParaphraseOut, max_tokens=512)
    except LLMError as e:
        return item.question_template, f"llm_error: {e}"
    q = out.question.strip()
    missing = [n for n in protected_spans(item) + required_names if n and n not in q]
    if missing:
        return item.question_template, f"dropped entity names: {missing}"
    return q, None


# ---- review & freeze ----------------------------------------------------------------


def read_items(path: str | Path) -> list[BenchItem]:
    p = Path(path)
    return [BenchItem(**json.loads(x)) for x in p.read_text(encoding="utf-8").splitlines() if x.strip()]


def write_items(path: str | Path, items: list[BenchItem]) -> None:
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    with p.open("w", encoding="utf-8") as f:
        for it in items:
            f.write(it.model_dump_json() + "\n")


def read_reviews(path: str | Path) -> list[Review]:
    p = Path(path)
    if not p.exists():
        return []
    return [Review(**json.loads(x)) for x in p.read_text(encoding="utf-8").splitlines() if x.strip()]


def append_review(path: str | Path, review: Review) -> None:
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    with p.open("a", encoding="utf-8") as f:
        f.write(review.model_dump_json() + "\n")


def make_review(item_id: str, status: str, reviewer: str, notes: str = "", **edits) -> Review:
    return Review(item_id=item_id, status=status, reviewer=reviewer, notes=notes, timestamp=datetime.now(UTC).isoformat(), **edits)  # type: ignore[arg-type]


EDIT_NOTE = " [gold_answer edited in review: required_path and gold_chunk_ids were not regenerated]"


def apply_reviews(items: list[BenchItem], reviews: list[Review], linker=None) -> list[BenchItem]:
    """Apply every review in timestamp order (stable for equal timestamps).

    Status, reviewer and notes come from the latest review; edits accumulate, so a later
    "accepted" keeps earlier edits. An edited gold answer is re-linked to graph nodes with
    ``linker`` (names that do not link are kept as literal strings); the item notes that its
    required path and gold chunks were not regenerated.
    """
    by_item: dict[str, list[Review]] = defaultdict(list)
    for r in sorted(reviews, key=lambda r: r.timestamp):
        by_item[r.item_id].append(r)
    out = []
    for it in items:
        rs = by_item.get(it.item_id)
        if not rs:
            out.append(it)
            continue
        upd: dict = {}
        for r in rs:
            upd.update(review_status=r.status, reviewer=r.reviewer, review_notes=r.notes)
            if r.question is not None:
                upd["question"] = r.question
            if r.gold_decision is not None:
                upd["gold_decision"] = r.gold_decision
            if r.gold_answer is not None:
                upd["gold_answer"] = r.gold_answer
                ids = []
                for name in r.gold_answer:
                    lk = linker.link_name(name) if linker is not None else None
                    ids.append(lk.node_id if lk else name)
                upd["gold_answer_ids"] = ids
                if EDIT_NOTE not in it.annotation_notes:
                    upd["annotation_notes"] = it.annotation_notes + EDIT_NOTE
        out.append(it.model_copy(update=upd))
    return out


def freeze(items: list[BenchItem], reviews: list[Review], linker=None) -> tuple[list[BenchItem], dict]:
    reviewed = apply_reviews(items, reviews, linker)
    edited_ids = {r.item_id for r in reviews if r.status == "edited"}
    release = [it for it in reviewed if it.review_status in ("accepted", "edited")]
    by_cat: dict[str, int] = defaultdict(int)
    for it in release:
        by_cat[it.category] += 1
    manifest = {
        "frozen_utc": datetime.now(UTC).isoformat(),
        "candidates": len(items),
        "reviewed": sum(1 for it in reviewed if it.review_status != "unreviewed"),
        "released": len(release),
        "rejected": sum(1 for it in reviewed if it.review_status == "rejected"),
        "edited": sum(1 for it in release if it.item_id in edited_ids),
        "by_category": dict(by_cat),
        "reviewers": sorted({it.reviewer for it in release if it.reviewer}),
    }
    return release, manifest