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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ResearchBench command line interface."""

from __future__ import annotations

import argparse
import sys
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Any, Callable

from .builders import build_all, validate_data_dir
from .clients import ModelClient, make_client
from .data_io import iter_jsonl, read_json, write_json, write_jsonl
from .generation import estimate_generation_model_calls, generation_score_prompt, parse_matched_score, run_generation_record
from .ranking import run_ranking_record, score_ranking
from .retrieve import run_retrieve_record, score_retrieve


RANKING_ORDER_ALIASES = {
    "res": "res",
    "gold-first": "res",
    "fan_1_res": "fan_1_res",
    "negative-first": "fan_1_res",
    "both": "both",
}


def _load_jsonl(path: str | Path) -> list[dict[str, Any]]:
    return list(iter_jsonl(path))


def _progress(label: str, done: int, total: int, started_at: float) -> None:
    if total <= 0:
        return
    width = 28
    ratio = done / total
    filled = int(width * ratio)
    elapsed = max(time.time() - started_at, 1e-6)
    rate = done / elapsed if done else 0.0
    eta = (total - done) / rate if rate else 0.0
    bar = "#" * filled + "." * (width - filled)
    sys.stderr.write(f"\r{label} [{bar}] {done}/{total} {ratio:6.1%} | {rate:5.2f}/s | ETA {eta:5.1f}s")
    if done >= total:
        sys.stderr.write("\n")
    sys.stderr.flush()


class ProgressTracker:
    def __init__(self, label: str, total: int, *, min_interval: float = 0.1) -> None:
        self.label = label
        self.total = max(total, 1)
        self.done = 0
        self.started_at = time.time()
        self.min_interval = min_interval
        self._last_rendered_at = 0.0
        self._last_rendered_done = -1
        self._last_rendered_total = -1
        self._lock = threading.Lock()
        self._render(force=True)

    def _render(self, *, force: bool = False) -> None:
        now = time.time()
        if not force and self.done < self.total and now - self._last_rendered_at < self.min_interval:
            return
        if not force and self.done == self._last_rendered_done:
            return
        _progress(self.label, self.done, self.total, self.started_at)
        self._last_rendered_at = now
        self._last_rendered_done = self.done
        self._last_rendered_total = self.total

    def advance(self, amount: int = 1) -> None:
        with self._lock:
            self.done += amount
            if self.done > self.total:
                self.total = self.done
            self._render(force=self.done >= self.total)

    def finish(self) -> None:
        with self._lock:
            if self.done < self.total:
                self.total = self.done or self.total
            if self.done != self._last_rendered_done or self.total != self._last_rendered_total:
                self._render(force=True)


class ProgressModelClient(ModelClient):
    def __init__(self, get_client: Callable[[], ModelClient], tracker: ProgressTracker, *, is_mock: bool = False) -> None:
        self._get_client = get_client
        self._tracker = tracker
        self.is_mock = is_mock

    def generate(self, prompt: str, *, temperature: float = 0.0) -> str:
        try:
            return self._get_client().generate(prompt, temperature=temperature)
        finally:
            self._tracker.advance()


def _run_with_progress(
    rows: list[dict[str, Any]],
    worker: Callable[[dict[str, Any]], dict[str, Any]],
    *,
    label: str,
    concurrency: int,
) -> list[dict[str, Any]]:
    total = len(rows)
    if total == 0:
        return []
    concurrency = max(1, concurrency)
    started_at = time.time()
    _progress(label, 0, total, started_at)
    results: list[dict[str, Any] | None] = [None] * total
    if concurrency == 1:
        for idx, row in enumerate(rows):
            try:
                results[idx] = worker(row)
            except Exception as exc:
                sample_id = row.get("sample_id", f"row-{idx}")
                raise RuntimeError(f"{label} failed on sample {sample_id}") from exc
            _progress(label, idx + 1, total, started_at)
    else:
        with ThreadPoolExecutor(max_workers=concurrency) as executor:
            future_to_item = {executor.submit(worker, row): (idx, row) for idx, row in enumerate(rows)}
            done = 0
            for future in as_completed(future_to_item):
                idx, row = future_to_item[future]
                try:
                    results[idx] = future.result()
                except Exception as exc:
                    sample_id = row.get("sample_id", f"row-{idx}")
                    raise RuntimeError(f"{label} failed on sample {sample_id}") from exc
                done += 1
                _progress(label, done, total, started_at)
    return [item for item in results if item is not None]


def _run_ordered_concurrent(
    rows: list[dict[str, Any]],
    worker: Callable[[dict[str, Any]], dict[str, Any]],
    *,
    label: str,
    concurrency: int,
) -> list[dict[str, Any]]:
    total = len(rows)
    if total == 0:
        return []
    concurrency = max(1, concurrency)
    results: list[dict[str, Any] | None] = [None] * total
    if concurrency == 1:
        for idx, row in enumerate(rows):
            try:
                results[idx] = worker(row)
            except Exception as exc:
                sample_id = row.get("sample_id", f"row-{idx}")
                raise RuntimeError(f"{label} failed on sample {sample_id}") from exc
    else:
        with ThreadPoolExecutor(max_workers=concurrency) as executor:
            future_to_item = {executor.submit(worker, row): (idx, row) for idx, row in enumerate(rows)}
            for future in as_completed(future_to_item):
                idx, row = future_to_item[future]
                try:
                    results[idx] = future.result()
                except Exception as exc:
                    sample_id = row.get("sample_id", f"row-{idx}")
                    raise RuntimeError(f"{label} failed on sample {sample_id}") from exc
    return [item for item in results if item is not None]


def _thread_local_client_getter(model: str, api_key: str | None, base_url: str | None):
    local = threading.local()

    def get_client():
        client = getattr(local, "client", None)
        if client is None:
            client = make_client(model, api_key=api_key, base_url=base_url)
            local.client = client
        return client

    return get_client


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


def _print_retrieve_summary(summary: dict[str, Any]) -> None:
    label_names = {
        "gold": "Gold inspiration hit ratio",
        "negative_t1": "Negative tier 1 selection ratio",
        "negative_t2": "Negative tier 2 selection ratio",
        "negative_t3": "Negative tier 3 selection ratio",
        "negative_unknown": "Unknown-label candidate selection ratio",
    }
    round_names = {
        "round1": "Round 1: kept 15 of 75 candidates",
        "round2": "Round 2: kept 3 of 75 candidates",
    }
    print("Retrieve score summary")
    for round_id in ("round1", "round2"):
        if round_id not in summary:
            continue
        print(f"- {round_names.get(round_id, round_id)}")
        for label, value in sorted(summary[round_id].items()):
            name = label_names.get(label, label)
            print(f"  - {name}: {_format_ratio(float(value))}")


def _print_generation_summary(summary: dict[str, Any]) -> None:
    print("Hypothesis generation score summary")
    print(f"- Scored samples: {summary.get('count', 0)}")
    print(f"- Average matched score (0-5): {float(summary.get('average_score', 0.0)):.4f}")
    print(f"- Generation accuracy (average score / 5): {_format_ratio(float(summary.get('accuracy', 0.0)))}")


def _print_ranking_summary(summary: dict[str, Any]) -> None:
    order_names = {
        "res": "Gold-first order accuracy (gold is candidate 1)",
        "fan_1_res": "Negative-first order accuracy (gold is candidate 2)",
    }
    sum_names = {
        "0": "Gold wins in neither order",
        "1": "Gold wins in exactly one order",
        "2": "Gold wins in both orders",
    }
    pair_names = {
        "both_correct": "Both orders choose gold",
        "self_contradictory": "Orders disagree",
        "both_wrong": "Both orders choose negative",
    }
    print("Hypothesis ranking score summary")
    print(f"- Overall directional accuracy: {_format_ratio(float(summary.get('overall_accuracy', 0.0)))}")
    for order, value in summary.get("order_accuracy", {}).items():
        print(f"- {order_names.get(order, order)}: {_format_ratio(float(value))}")
    if summary.get("sum_ratios"):
        print("- Sample-level two-order outcomes")
        for key in ("0", "1", "2"):
            if key in summary["sum_ratios"]:
                count = summary.get("sum_counts", {}).get(int(key), summary.get("sum_counts", {}).get(key, 0))
                print(f"  - {sum_names[key]}: {_format_ratio(float(summary['sum_ratios'][key]))}; count={count}")
    if summary.get("pair_ratios"):
        print("- Pair-level order-consistency outcomes")
        for key in ("both_correct", "self_contradictory", "both_wrong"):
            if key in summary["pair_ratios"]:
                count = summary.get("pair_counts", {}).get(key, 0)
                print(f"  - {pair_names[key]}: {_format_ratio(float(summary['pair_ratios'][key]))}; count={count}")


def _print_build_summary(summary: dict[str, Any], out_dir: str | Path) -> None:
    print("Data build summary")
    print(f"- Papers: {summary.get('papers', 0)}")
    print(f"- Retrieve tasks: {summary.get('retrieve', 0)}")
    print(f"- Generation tasks: {summary.get('generation', 0)}")
    print(f"- Ranking tasks: {summary.get('ranking', 0)}")
    print(f"- Retrieve build issues: {summary.get('retrieve_issues', 0)}")
    print(f"- Ranking build issues: {summary.get('ranking_issues', 0)}")
    print(f"- Validation reports: {Path(out_dir) / 'validation'}")


def cmd_build_data(args: argparse.Namespace) -> int:
    summary = build_all(args.source_root, args.out)
    _print_build_summary(summary, args.out)
    return 0


def cmd_validate(args: argparse.Namespace) -> int:
    ok, issues = validate_data_dir(args.data_dir)
    if ok:
        print("Validation passed.")
        return 0
    report = Path(args.data_dir) / "validation" / "validation_issues.csv"
    print(f"Validation failed with {len(issues)} issue(s). See {report}.")
    return 1


def cmd_run_retrieve(args: argparse.Namespace) -> int:
    rows = _load_jsonl(args.data)
    get_client = _thread_local_client_getter(args.model, args.api_key, args.base_url)
    preds = _run_with_progress(
        rows,
        lambda row: {**run_retrieve_record(row, get_client()), "model": args.model},
        label="retrieve samples",
        concurrency=args.concurrency,
    )
    write_jsonl(args.out, preds)
    print(f"Wrote {len(preds)} retrieve predictions to {args.out}")
    return 0


def cmd_score_retrieve(args: argparse.Namespace) -> int:
    result = score_retrieve(_load_jsonl(args.pred), _load_jsonl(args.data))
    out = args.out or Path(args.pred).with_suffix(".score.json")
    write_json(out, result)
    _print_retrieve_summary(result["summary"])
    return 0


def cmd_run_generate(args: argparse.Namespace) -> int:
    rows = _load_jsonl(args.data)
    get_client = _thread_local_client_getter(args.model, args.api_key, args.base_url)
    estimated_calls = sum(
        estimate_generation_model_calls(
            row,
            num_mutations=args.num_mutations,
            num_itr_self_refine=args.num_itr_self_refine,
            max_inspiration_search_steps=args.max_inspiration_search_steps,
            num_screening_window_size=args.num_screening_window_size,
            num_screening_keep_size=args.num_screening_keep_size,
            recom_num_beam_size=args.recom_num_beam_size,
        )
        for row in rows
    )
    tracker = ProgressTracker("generation API calls", estimated_calls)

    def make_progress_client() -> ModelClient:
        return ProgressModelClient(get_client, tracker, is_mock=args.model == "mock")

    def worker(row: dict[str, Any]) -> dict[str, Any]:
        return {
            **run_generation_record(
                row,
                make_progress_client(),
                num_mutations=args.num_mutations,
                num_itr_self_refine=args.num_itr_self_refine,
                max_inspiration_search_steps=args.max_inspiration_search_steps,
                use_background_survey=args.use_background_survey,
                num_screening_window_size=args.num_screening_window_size,
                num_screening_keep_size=args.num_screening_keep_size,
                recom_num_beam_size=args.recom_num_beam_size,
                inner_concurrency=args.inner_concurrency,
                client_factory=make_progress_client,
            ),
            "model": args.model,
        }

    try:
        preds = _run_ordered_concurrent(rows, worker, label="generate", concurrency=args.concurrency)
    finally:
        tracker.finish()
    write_jsonl(args.out, preds)
    print(f"Wrote {len(preds)} generation predictions to {args.out}")
    return 0


def cmd_score_generate(args: argparse.Namespace) -> int:
    predictions = _load_jsonl(args.pred)
    data = _load_jsonl(args.data)
    result = _score_generation_concurrent(
        predictions,
        data,
        judge_model=args.judge_model,
        api_key=args.api_key,
        base_url=args.base_url,
        concurrency=args.concurrency,
    )
    out = args.out or Path(args.pred).with_suffix(".score.json")
    write_json(out, result)
    _print_generation_summary(result["summary"])
    return 0


def cmd_run_rank(args: argparse.Namespace) -> int:
    rows = _load_jsonl(args.data)
    get_client = _thread_local_client_getter(args.model, args.api_key, args.base_url)
    order = RANKING_ORDER_ALIASES[args.order]
    preds = _run_with_progress(
        rows,
        lambda row: {**run_ranking_record(row, get_client(), order=order), "model": args.model},
        label="ranking samples",
        concurrency=args.concurrency,
    )
    write_jsonl(args.out, preds)
    print(f"Wrote {len(preds)} ranking predictions to {args.out}")
    return 0


def _score_generation_concurrent(
    predictions: list[dict[str, Any]],
    data: list[dict[str, Any]],
    *,
    judge_model: str,
    api_key: str | None,
    base_url: str | None,
    concurrency: int,
) -> dict[str, Any]:
    data_by_id = {row["sample_id"]: row for row in data}
    get_client = _thread_local_client_getter(judge_model, api_key, base_url)

    def worker(pred: dict[str, Any]) -> dict[str, Any]:
        record = data_by_id.get(pred.get("sample_id"))
        if not record:
            return {}
        score = pred.get("matched_score")
        reason = pred.get("matched_score_reason", "")
        if score is None:
            key_points = record.get("gold_key_points") or record["gold_hypothesis"]
            response = get_client().generate(
                generation_score_prompt(pred.get("final_hypothesis", ""), record["gold_hypothesis"], key_points),
                temperature=0.0,
            )
            score = parse_matched_score(response)
            reason = response
        if score is None:
            return {}
        score = float(score)
        return {
            "sample_id": pred["sample_id"],
            "matched_score": score,
            "accuracy": score / 5.0,
            "reason": reason,
        }

    per_sample = [
        item
        for item in _run_with_progress(predictions, worker, label="generation scoring", concurrency=concurrency)
        if item
    ]
    scores = [item["matched_score"] for item in per_sample]
    average_score = sum(scores) / len(scores) if scores else 0.0
    return {
        "summary": {
            "count": len(scores),
            "average_score": average_score,
            "accuracy": average_score / 5.0,
        },
        "per_sample": per_sample,
    }


def cmd_score_rank(args: argparse.Namespace) -> int:
    result = score_ranking(_load_jsonl(args.pred))
    out = args.out or Path(args.pred).with_suffix(".score.json")
    write_json(out, result)
    _print_ranking_summary(result["summary"])
    return 0


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="researchbench")
    sub = parser.add_subparsers(dest="command", required=True)

    build = sub.add_parser("build-data")
    build.add_argument("--source-root", required=True)
    build.add_argument("--out", required=True)
    build.set_defaults(func=cmd_build_data)

    validate = sub.add_parser("validate")
    validate.add_argument("data_dir")
    validate.set_defaults(func=cmd_validate)

    def add_model_args(p: argparse.ArgumentParser) -> None:
        p.add_argument("--model", "--model-name", dest="model", required=True)
        p.add_argument("--api-key")
        p.add_argument("--base-url")
        p.add_argument("--concurrency", type=int, default=15, help="number of samples to evaluate concurrently")
        p.add_argument("--out", required=True)

    run_retrieve = sub.add_parser("run-retrieve")
    run_retrieve.add_argument("--data", required=True)
    add_model_args(run_retrieve)
    run_retrieve.set_defaults(func=cmd_run_retrieve)

    score_retrieve_parser = sub.add_parser("score-retrieve")
    score_retrieve_parser.add_argument("--pred", required=True)
    score_retrieve_parser.add_argument("--data", required=True)
    score_retrieve_parser.add_argument("--out")
    score_retrieve_parser.set_defaults(func=cmd_score_retrieve)

    run_generate = sub.add_parser("run-generate")
    run_generate.add_argument("--data", required=True)
    run_generate.add_argument("--num-mutations", type=int, default=2)
    run_generate.add_argument("--num-itr-self-refine", type=int, default=2)
    run_generate.add_argument("--max-inspiration-search-steps", type=int, default=3)
    run_generate.add_argument("--use-background-survey", action="store_true")
    run_generate.add_argument("--num-screening-window-size", type=int, default=12)
    run_generate.add_argument("--num-screening-keep-size", type=int, default=3)
    run_generate.add_argument("--recom-num-beam-size", type=int, default=15)
    run_generate.add_argument(
        "--inner-concurrency",
        type=int,
        default=1,
        help="within-sample concurrency for independent generation branches",
    )
    add_model_args(run_generate)
    run_generate.set_defaults(func=cmd_run_generate)

    score_generate_parser = sub.add_parser("score-generate")
    score_generate_parser.add_argument("--pred", required=True)
    score_generate_parser.add_argument("--data", required=True)
    score_generate_parser.add_argument("--judge-model", required=True)
    score_generate_parser.add_argument("--api-key")
    score_generate_parser.add_argument("--base-url")
    score_generate_parser.add_argument("--concurrency", type=int, default=15, help="number of samples to judge concurrently")
    score_generate_parser.add_argument("--out")
    score_generate_parser.set_defaults(func=cmd_score_generate)

    run_rank = sub.add_parser("run-rank")
    run_rank.add_argument("--data", required=True)
    run_rank.add_argument(
        "--order",
        choices=["gold-first", "negative-first", "res", "fan_1_res", "both"],
        default="both",
        help=(
            "comparison order: gold-first means gold is candidate 1; "
            "negative-first means the negative hypothesis is candidate 1; both runs both orders"
        ),
    )
    add_model_args(run_rank)
    run_rank.set_defaults(func=cmd_run_rank)

    score_rank_parser = sub.add_parser("score-rank")
    score_rank_parser.add_argument("--pred", required=True)
    score_rank_parser.add_argument("--out")
    score_rank_parser.set_defaults(func=cmd_score_rank)

    return parser


def main(argv: list[str] | None = None) -> int:
    parser = build_parser()
    args = parser.parse_args(argv)
    return args.func(args)


if __name__ == "__main__":
    raise SystemExit(main())