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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
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
"""
DataClaw — Host-side benchmark orchestrator.

Runs each task in an isolated Docker container. The host manages container
lifecycle, workspace injection, agent execution, LLM-judge grading, and
result collection.

Usage:
    python dataclaw/eval/run_batch.py --model openrouter/anthropic/claude-sonnet-4.6
    python dataclaw/eval/run_batch.py --model ... --suite task_001,task_002
    python dataclaw/eval/run_batch.py --model ... --parallel 4
    python dataclaw/eval/run_batch.py --task tasks/task_001_xxx.md
"""

from __future__ import annotations

import argparse
import json
import logging
import os
import re
import subprocess
import sys
import tempfile
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional

try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass

# Allow imports from project root
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from dataclaw.lib_tasks import Task, TaskLoader
from dataclaw.utils.docker_utils import (
    JUDGE_CUSTOM_API_KEY,
    JUDGE_CUSTOM_BASE_URL,
    JUDGE_CUSTOM_MODEL_ID,
    close_proc_log,
    collect_output,
    collect_transcript,
    detect_transcript_errors,
    extract_usage_from_jsonl,
    onboard_openclaw,
    register_custom_provider,
    remove_container,
    set_model,
    setup_workspace,
    start_container,
    start_gateway,
)
from dataclaw.utils.grading import GradeResult, grade_task, _run_judge_in_container
from dataclaw.utils.process_grading import (
    parse_trajectory,
    compute_efficiency,
    build_gpr_judge_prompt,
    parse_gpr_judge_response,
    compute_tgpr,
)

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Configuration from environment
# ---------------------------------------------------------------------------

ROOT_DIR = Path(__file__).resolve().parent.parent.parent
TASKS_DIR = ROOT_DIR / os.environ.get("TASKS_SUBDIR", "tasks")
ASSETS_DIR = ROOT_DIR / "assets"
OUTPUT_DIR = ROOT_DIR / os.environ.get("OUTPUT_SUBDIR", "output")

DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "")
DEFAULT_PARALLEL = int(os.environ.get("DEFAULT_PARALLEL", "1"))
DEFAULT_JUDGE_MODEL = os.environ.get("JUDGE_MODEL", "openrouter/anthropic/claude-opus-4.5")
TIMEOUT_MULTIPLIER = float(os.environ.get("BENCHMARK_TIMEOUT_MULTIPLIER", "1.0"))
BENCHMARK_RUNS = int(os.environ.get("BENCHMARK_RUNS", "1"))
TMP_WORKSPACE = os.environ.get("TMP_WORKSPACE", "/tmp_workspace")

OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _slugify_model(model_id: str) -> str:
    return re.sub(r"[^a-zA-Z0-9.\-_]", "_", model_id.rsplit("/", 1)[-1])


def _get_git_version() -> str:
    try:
        r = subprocess.run(
            ["git", "rev-parse", "--short", "HEAD"],
            capture_output=True, text=True, encoding="utf-8", timeout=2, check=False, cwd=ROOT_DIR,
        )
        return r.stdout.strip() if r.returncode == 0 else ""
    except (subprocess.SubprocessError, FileNotFoundError, OSError):
        return ""


def _validate_openrouter_model(model_id: str) -> bool:
    """Basic check that the model exists on OpenRouter (skippable)."""
    if os.environ.get("DATACLAW_SKIP_OPENROUTER_MODEL_VALIDATION", "").strip().lower() in (
        "1", "true", "yes",
    ):
        return True
    if os.environ.get("OPENCLAW_CUSTOM_BASE_URL", "").strip():
        return True

    bare = model_id
    if bare.startswith("openrouter/"):
        bare = bare[len("openrouter/"):]
    if bare.startswith("bailian/") or "/" not in bare:
        return True
    if not OPENROUTER_API_KEY:
        logger.warning("OPENROUTER_API_KEY not set, skipping model validation")
        return True

    from urllib import error, request as urlreq
    encoded = bare.replace("/", "%2F")
    url = f"https://openrouter.ai/api/v1/models/{encoded}"
    req = urlreq.Request(url, headers={
        "Authorization": f"Bearer {OPENROUTER_API_KEY}",
        "HTTP-Referer": "https://github.com/GTMLLab/DataClaw",
        "X-Title": "DataClaw",
    }, method="GET")
    try:
        with urlreq.urlopen(req, timeout=10):
            return True
    except error.HTTPError as exc:
        if exc.code == 404:
            logger.error("Model '%s' not found on OpenRouter", bare)
            return False
        return True
    except (error.URLError, OSError):
        return True


# ---------------------------------------------------------------------------
# Progress file helpers (for --resume support)
# ---------------------------------------------------------------------------

_progress_lock = threading.Lock()


def _progress_path(model_slug: str) -> Path:
    return OUTPUT_DIR / f"progress_{model_slug}.json"


def _load_progress(path: Path) -> Optional[Dict[str, Any]]:
    """Load progress file, returning the parsed dict or None."""
    if not path.exists():
        return None
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except (json.JSONDecodeError, OSError) as exc:
        logger.warning("Failed to read progress file %s: %s", path, exc)
        return None


def _save_progress(
    path: Path,
    model: str,
    suite: str,
    runs: int,
    completed: List[Dict[str, Any]],
) -> None:
    """Atomically write progress file (temp file + os.replace)."""
    data = {
        "model": model,
        "suite": suite,
        "runs": runs,
        "completed": completed,
    }
    path.parent.mkdir(parents=True, exist_ok=True)
    tmp_path = path.with_suffix(".tmp")
    tmp_path.write_text(
        json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8"
    )
    os.replace(str(tmp_path), str(path))


# ---------------------------------------------------------------------------
# Process grading helpers
# ---------------------------------------------------------------------------

PROCESS_JUDGE_TIMEOUT_SECONDS = 180


def _load_gold_process_data(task: Task) -> Optional[Dict[str, Any]]:
    """Load gold process fields (steps, milestone, steps_num) from qa_gold."""
    gold_file = task.frontmatter.get("gold_file", "")
    if not gold_file:
        return None
    gold_path = ASSETS_DIR / gold_file
    if not gold_path.exists():
        logger.warning("Gold file not found: %s", gold_path)
        return None
    try:
        data = json.loads(gold_path.read_text(encoding="utf-8"))
    except (json.JSONDecodeError, OSError) as exc:
        logger.warning("Failed to read gold file %s: %s", gold_path, exc)
        return None
    if not data.get("milestone") or not data.get("steps"):
        return None
    return data


GPR_MAX_ATTEMPTS = 5


def _extract_assistant_text(transcript_path: Path) -> str:
    """Concatenate all assistant text content from a transcript JSONL.

    Returns empty string on missing file or read error (callers treat
    empty result as an L2 failure).
    """
    if not transcript_path.exists():
        return ""
    try:
        text = transcript_path.read_text(encoding="utf-8", errors="replace")
    except OSError:
        return ""
    out = ""
    for line in text.splitlines():
        line = line.strip()
        if not line:
            continue
        try:
            entry = json.loads(line)
        except json.JSONDecodeError:
            continue
        if entry.get("type") != "message":
            continue
        msg = entry.get("message", {})
        if msg.get("role") != "assistant":
            continue
        for item in msg.get("content", []) or []:
            if isinstance(item, dict) and item.get("type") == "text":
                out += item.get("text", "") or ""
    return out


def _run_process_grading(
    *,
    container_id: str,
    task_id: str,
    transcript_path: Path,
    gold_data: Dict[str, Any],
    outcome_score: float,
    output_dir: Path,
) -> Dict[str, Any]:
    """Run process grading conditional on outcome score.

    Full-score tasks (score >= 1.0) get only Efficiency. Incorrect tasks
    (score < 1.0) get only GPR (via LLM Judge, with GPR_MAX_ATTEMPTS retries
    across three layers: runtime / transcript / parse), TGPR and TPE.
    Raises RuntimeError if the GPR judge fails every attempt.
    """
    milestones = gold_data.get("milestone", {})
    gold_steps = gold_data.get("steps", [])
    steps_num = gold_data.get("steps_num", len(gold_steps))

    if outcome_score >= 1.0:
        # --- Full score: Efficiency only ---
        steps = parse_trajectory(transcript_path)
        eff = compute_efficiency(steps, steps_num)
        process_scores: Dict[str, Any] = {"efficiency": eff.to_dict()}
        (output_dir / "process_score.json").write_text(
            json.dumps(process_scores, indent=2, ensure_ascii=False), encoding="utf-8"
        )
        logger.info(
            "[%s] Process: efficiency=%.2f (full score, GPR/TGPR/TPE skipped)",
            task_id,
            eff.efficiency if eff.efficiency is not None else 0.0,
        )
        return process_scores

    # --- Incorrect: GPR + TGPR + TPE only ---
    steps = parse_trajectory(transcript_path)
    gpr_prompt = build_gpr_judge_prompt(
        steps=steps,
        milestones=milestones,
        gold_steps=gold_steps,
        final_answer_correct=False,
    )

    gpr_result = None
    last_err: Optional[str] = None

    for attempt in range(1, GPR_MAX_ATTEMPTS + 1):
        # Clear previous judge sessions before each attempt
        subprocess.run(
            ["docker", "exec", container_id, "/bin/bash", "-c",
             "rm -rf /root/.openclaw/agents/judge/sessions/*"],
            capture_output=True, text=True, encoding="utf-8",
        )

        # L1: runtime
        try:
            _run_judge_in_container(container_id, gpr_prompt)
        except RuntimeError as exc:
            last_err = f"runtime: {exc}"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        gpr_transcript = collect_transcript(
            container_id, output_dir, agent_id="judge",
            output_filename="judge_process_chat.jsonl",
        )

        # L2: transcript validity
        if not gpr_transcript.exists():
            last_err = "transcript: file missing"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        tr_err = detect_transcript_errors(gpr_transcript)
        if tr_err:
            last_err = f"transcript: {tr_err}"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        gpr_raw_text = _extract_assistant_text(gpr_transcript)
        if not gpr_raw_text.strip():
            last_err = "transcript: empty assistant response"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        # L3: format — parse_gpr_judge_response falls back to a sentinel
        # GPRResult when required fields are missing; detect it explicitly.
        try:
            parsed_gpr = parse_gpr_judge_response(gpr_raw_text, milestones)
        except Exception as exc:
            last_err = f"format: parse failed: {exc}"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        if "could not be parsed" in (parsed_gpr.chain_summary or ""):
            last_err = "format: judge response missing required fields (milestones)"
            logger.warning("[%s] GPR attempt %d/%d failed — %s",
                           task_id, attempt, GPR_MAX_ATTEMPTS, last_err)
            continue

        gpr_result = parsed_gpr
        logger.info("[%s] GPR judge succeeded on attempt %d", task_id, attempt)
        break

    if gpr_result is None:
        raise RuntimeError(
            f"GPR judge failed after {GPR_MAX_ATTEMPTS} attempts: {last_err}"
        )

    tgpr_result = compute_tgpr(gpr_result, s_gold=steps_num)

    process_scores = {
        "gpr": gpr_result.to_dict(),
        "tgpr": tgpr_result.to_dict(),
    }
    (output_dir / "process_score.json").write_text(
        json.dumps(process_scores, indent=2, ensure_ascii=False), encoding="utf-8"
    )
    logger.info(
        "[%s] Process: GPR=%.2f, TGPR=%.2f, TPE=%.2f (incorrect, efficiency skipped)",
        task_id,
        gpr_result.gpr,
        tgpr_result.tgpr,
        tgpr_result.tpe,
    )
    return process_scores


# ---------------------------------------------------------------------------
# Single-task execution
# ---------------------------------------------------------------------------

def run_single_task(
    task: Task,
    model: str,
    judge_model: str,
    timeout_multiplier: float,
) -> Dict[str, Any]:
    """Execute a single task in an isolated container. Thread-safe."""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M")
    run_id = uuid.uuid4().hex[:6]
    short_model = _slugify_model(model)
    suffix = f"{short_model}_{timestamp}_{run_id}"
    container_id = f"{task.task_id}_{suffix}"

    # Truncate container name to Docker's 128-char limit
    if len(container_id) > 128:
        container_id = container_id[:128]

    output_dir = OUTPUT_DIR / task.task_id / suffix
    output_dir.mkdir(parents=True, exist_ok=True)

    result: Dict[str, Any] = {
        "task_id": task.task_id,
        "model": model,
        "scores": {},
        "grade": None,
        "process_grade": None,
        "usage": {},
        "error": None,
        "elapsed_time": 0.0,
    }
    errors: List[str] = []

    gateway_proc = None
    agent_proc = None
    timeout_seconds = task.timeout_seconds * timeout_multiplier
    start_time = time.perf_counter()

    try:
        # 1. Start container
        start_container(container_id)

        # 2. Inject workspace files
        if task.workspace_files:
            setup_workspace(container_id, task.workspace_files, ASSETS_DIR)

        # 3. Onboard OpenClaw
        onboard_openclaw(container_id)

        # 3.5 Register judge provider if using a separate endpoint
        if JUDGE_CUSTOM_BASE_URL and JUDGE_CUSTOM_API_KEY:
            register_custom_provider(
                container_id,
                JUDGE_CUSTOM_BASE_URL,
                JUDGE_CUSTOM_API_KEY,
                JUDGE_CUSTOM_MODEL_ID or judge_model,
            )

        # 4. Start gateway
        gateway_proc = start_gateway(container_id, output_dir / "gateway.log")

        # 5. Set model
        set_model(container_id, model)

        # 6. Run agent — use docker cp to bypass Windows cmd-line limit
        fd, tmp_path = tempfile.mkstemp(suffix=".txt")
        with os.fdopen(fd, "w", encoding="utf-8") as f:
            f.write(task.prompt)
        subprocess.run(
            ["docker", "cp", tmp_path,
             f"{container_id}:/tmp/agent_prompt.txt"],
            capture_output=True, text=True, encoding="utf-8",
        )
        os.unlink(tmp_path)

        agent_bash = (
            f"prompt=$(cat /tmp/agent_prompt.txt) && "
            f"cd {TMP_WORKSPACE} && "
            f"openclaw agent --session-id chat "
            f"--timeout {int(timeout_seconds)} --message \"$prompt\""
        )

        log_file = (output_dir / "agent.log").open("w", encoding="utf-8")
        agent_proc = subprocess.Popen(
            ["docker", "exec", container_id, "/bin/bash", "-c", agent_bash],
            stdout=log_file,
            stderr=subprocess.STDOUT,
            encoding="utf-8",
        )
        agent_proc._log_file = log_file  # type: ignore[attr-defined]

        logger.info("[%s] Agent running (timeout=%ds)", container_id, int(timeout_seconds))
        try:
            agent_proc.wait(timeout=timeout_seconds)
            elapsed = time.perf_counter() - start_time
            logger.info("[%s] Agent finished (%.1fs, exit=%s)",
                        container_id, elapsed, agent_proc.returncode)
        except subprocess.TimeoutExpired:
            elapsed = timeout_seconds
            logger.info("[%s] Agent timed out", container_id)
            agent_proc.kill()
            agent_proc.wait()

        # 7. Collect transcript and compute usage
        transcript_path = collect_transcript(container_id, output_dir)
        usage = extract_usage_from_jsonl(transcript_path)
        usage["elapsed_time"] = round(time.perf_counter() - start_time, 2)
        result["usage"] = usage
        (output_dir / "usage.json").write_text(
            json.dumps(usage, indent=2, ensure_ascii=False), encoding="utf-8"
        )

        # 7.5 Check for API errors in transcript BEFORE grading
        transcript_err = detect_transcript_errors(transcript_path)
        if transcript_err:
            logger.warning("[%s] %s — skipping judge", container_id, transcript_err)
            errors.append(transcript_err)
        else:
            # 8. LLM Judge grading
            grade = None
            try:
                grade = grade_task(
                    container_id=container_id,
                    task_id=task.task_id,
                    task_prompt=task.prompt,
                    expected_behavior=task.expected_behavior,
                    grading_criteria=task.grading_criteria,
                    llm_judge_rubric=task.llm_judge_rubric,
                    agent_transcript_path=transcript_path,
                    output_dir=output_dir,
                    judge_model=judge_model,
                )
                result["grade"] = grade.to_dict()
                result["scores"] = grade.breakdown
                score_path = output_dir / "score.json"
                score_path.write_text(
                    json.dumps(grade.to_dict(), indent=2, ensure_ascii=False), encoding="utf-8"
                )
                logger.info("[%s] Grade: %.2f/%.2f", container_id, grade.score, grade.max_score)
            except Exception as exc:
                logger.error("[%s] Grading failed: %s", container_id, exc)
                errors.append(f"Grading failed: {exc}")

            # 8.5 Process grading — only if step 8 succeeded
            if not errors:
                try:
                    gold_data = _load_gold_process_data(task)
                    if gold_data is not None:
                        outcome_score = grade.score if grade else 0.0
                        process_scores = _run_process_grading(
                            container_id=container_id,
                            task_id=task.task_id,
                            transcript_path=transcript_path,
                            gold_data=gold_data,
                            outcome_score=outcome_score,
                            output_dir=output_dir,
                        )
                        result["process_grade"] = process_scores
                    else:
                        logger.info("[%s] No process annotations, skipping process grading", container_id)
                except Exception as exc:
                    logger.error("[%s] Process grading failed: %s", container_id, exc)
                    errors.append(f"Process grading failed: {exc}")

        # 9. Collect task output (always attempt — preserve artifacts for debugging)
        try:
            collect_output(container_id, output_dir)
        except Exception as exc:
            logger.warning("[%s] Output collection failed: %s", container_id, exc)
            errors.append(f"Output collection failed: {exc}")

    except Exception as exc:
        logger.error("[%s] Execution error: %s", container_id, exc)
        errors.append(f"Execution error: {exc}")

    finally:
        result["elapsed_time"] = round(time.perf_counter() - start_time, 2)
        if errors:
            result["error"] = "; ".join(errors)

        if gateway_proc is not None:
            try:
                gateway_proc.terminate()
            except Exception:
                pass
        for proc in [gateway_proc, agent_proc]:
            if proc is not None:
                try:
                    close_proc_log(proc)
                except Exception:
                    pass

        remove_container(container_id)
        logger.info("[%s] Container cleaned up", container_id)

    return result


# ---------------------------------------------------------------------------
# Summary & reporting
# ---------------------------------------------------------------------------

def _print_summary(results: List[Dict[str, Any]], model_name: str) -> None:
    print(f"\n{'#' * 60}")
    print(f"  Summary Report — {model_name}")
    print(f"{'#' * 60}")

    scored = 0
    total_score = 0.0
    for r in results:
        grade = r.get("grade")
        if r.get("error") or not grade:
            print(f"  X {r['task_id']}: {r.get('error', 'no grade')}")
            continue
        scored += 1
        total_score += grade.get("score", 0.0)
        pct = grade["score"] / grade["max_score"] * 100 if grade["max_score"] > 0 else 0
        print(f"  + {r['task_id']}: {grade['score']:.2f}/{grade['max_score']:.2f} ({pct:.0f}%)")

    error_count = sum(1 for r in results if r.get("error"))
    if scored:
        avg = total_score / scored
        print(f"\n  Scored: {scored}/{len(results)}  Average: {avg:.4f}")
        if error_count:
            print(f"  Errors: {error_count} task(s) skipped due to API errors")
    else:
        print("\n  No tasks scored successfully")
        if error_count:
            print(f"  Errors: {error_count} task(s) skipped due to API errors")

    valid = [r for r in results if not r.get("error")]
    total_out_tok = sum(r.get("usage", {}).get("output_tokens", 0) or 0 for r in valid)
    total_cost = sum(r.get("usage", {}).get("cost_usd", 0.0) or 0.0 for r in valid)
    print(f"  Total output tokens: {total_out_tok}  Total cost: ${total_cost:.4f}")

    # Process metrics summary — exclude error tasks
    proc = [
        r["process_grade"] for r in results
        if r.get("process_grade") and not r.get("error")
    ]
    if proc:
        eff_vals = [p["efficiency"]["efficiency"] for p in proc if p.get("efficiency", {}).get("efficiency") is not None]
        gpr_vals = [p["gpr"]["gpr"] for p in proc if "gpr" in p]
        tgpr_vals = [p["tgpr"]["tgpr"] for p in proc if "tgpr" in p]
        # TPE is added to the same TGPRResult dict; older runs may lack the key.
        tpe_vals = [p["tgpr"]["tpe"] for p in proc if "tgpr" in p and "tpe" in p["tgpr"]]
        print(f"\n  Process metrics ({len(proc)} tasks):")
        if eff_vals:
            print(f"    Avg Efficiency: {sum(eff_vals)/len(eff_vals):.4f}")
        if gpr_vals:
            print(f"    Avg GPR:        {sum(gpr_vals)/len(gpr_vals):.4f}")
        if tgpr_vals:
            print(f"    Avg TGPR:       {sum(tgpr_vals)/len(tgpr_vals):.4f}")
        if tpe_vals:
            print(f"    Avg TPE:        {sum(tpe_vals)/len(tpe_vals):.4f}")

    print("#" * 60)

    # Errored tasks — listed together for easy review
    errored = [r for r in results if r.get("error")]
    if errored:
        print(f"\n{'#' * 60}")
        print(f"  Errored Tasks ({len(errored)})")
        print(f"{'#' * 60}")
        for r in errored:
            print(f"  X {r['task_id']}: {r.get('error')}")
        print()
        print("  -> Run with --resume to retry these tasks")
        print("#" * 60)


def _write_global_summary(
    results: List[Dict[str, Any]],
    model: str,
    model_slug: str,
    suite: str,
    runs_per_task: int,
    task_objects: Optional[List[Task]] = None,
) -> Path:
    task_meta_map: Dict[str, Dict[str, Any]] = {}
    if task_objects:
        for t in task_objects:
            task_meta_map[t.task_id] = t.frontmatter

    aggregate: Dict[str, Any] = {
        "model": model,
        "benchmark_version": _get_git_version(),
        "timestamp": time.time(),
        "suite": suite,
        "runs_per_task": runs_per_task,
        "tasks": [],
    }

    agg_total_tokens = 0
    agg_total_cost = 0.0
    agg_total_requests = 0

    for r in results:
        grade = r.get("grade")
        usage = r.get("usage", {})
        score = grade.get("score", 0.0) if grade else 0.0
        elapsed = r.get("elapsed_time", 0.0)

        # Only count tokens/cost/requests from non-error tasks
        if not r.get("error"):
            agg_total_tokens += usage.get("total_tokens", 0) or 0
            agg_total_cost += usage.get("cost_usd", 0.0) or 0.0
            agg_total_requests += usage.get("request_count", 0) or 0

        entry: Dict[str, Any] = {
            "task_id": r["task_id"],
            "frontmatter": task_meta_map.get(r["task_id"], {}),
            "grade": grade,
            "process_grade": r.get("process_grade"),
            "grading": {"mean": score},
            "usage": usage,
            "error": r.get("error"),
            "elapsed_time": elapsed,
            "execution_time": elapsed,
        }
        aggregate["tasks"].append(entry)

    # Compute global score — exclude error tasks
    valid_results = [r for r in results if not r.get("error")]
    error_count = sum(1 for r in results if r.get("error"))
    grades = [r["grade"] for r in valid_results if r.get("grade")]
    if grades:
        total_score = sum(g["score"] for g in grades)
        max_score = sum(g["max_score"] for g in grades)
        overall = round(total_score / max_score, 4) if max_score > 0 else 0.0
        aggregate["overall_score"] = overall
        aggregate["total_score"] = total_score
        aggregate["max_score"] = max_score
    else:
        overall = 0.0
        aggregate["overall_score"] = 0
    aggregate["error_count"] = error_count
    aggregate["evaluated_count"] = len(results) - error_count

    aggregate["efficiency"] = {
        "total_tokens": agg_total_tokens,
        "total_cost_usd": round(agg_total_cost, 6),
        "total_requests": agg_total_requests,
    }

    # Aggregate process metrics — exclude error tasks
    process_results = [
        r["process_grade"] for r in results
        if r.get("process_grade") is not None and not r.get("error")
    ]
    if process_results:
        eff_values = [
            p["efficiency"]["efficiency"]
            for p in process_results
            if p.get("efficiency", {}).get("efficiency") is not None
        ]
        gpr_values = [p["gpr"]["gpr"] for p in process_results if "gpr" in p]
        tgpr_values = [p["tgpr"]["tgpr"] for p in process_results if "tgpr" in p]
        aggregate["process_metrics"] = {
            "tasks_with_process": len(process_results),
            "avg_efficiency": round(sum(eff_values) / len(eff_values), 4) if eff_values else None,
            "avg_gpr": round(sum(gpr_values) / len(gpr_values), 4) if gpr_values else None,
            "avg_tgpr": round(sum(tgpr_values) / len(tgpr_values), 4) if tgpr_values else None,
        }

    summary_path = OUTPUT_DIR / f"summary_{model_slug}.json"
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    summary_path.write_text(
        json.dumps(aggregate, indent=2, ensure_ascii=False), encoding="utf-8"
    )
    return summary_path


# ---------------------------------------------------------------------------
# CLI & main
# ---------------------------------------------------------------------------

def main() -> None:
    parser = argparse.ArgumentParser(
        description="DataClaw — per-task container benchmark orchestrator",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Run all tasks
  python dataclaw/eval/run_batch.py --model openrouter/anthropic/claude-sonnet-4.6

  # Run specific tasks
  python dataclaw/eval/run_batch.py --model ... --suite task_001,task_002

  # Run with parallelism
  python dataclaw/eval/run_batch.py --model ... --parallel 4

  # Run a single task file
  python dataclaw/eval/run_batch.py --task tasks/task_001_xxx.md

  # Resume an interrupted run
  python dataclaw/eval/run_batch.py --model ... --suite all --resume
""",
    )

    mode = parser.add_mutually_exclusive_group()
    mode.add_argument("--task", "-t", help="Path to a single task.md file")
    mode.add_argument(
        "--suite", "-s",
        default="all",
        help='Tasks to run: "all" or comma-separated task IDs (default: all)',
    )

    parser.add_argument(
        "--model", "-m",
        default=DEFAULT_MODEL,
        help=f"Model identifier (default: {DEFAULT_MODEL or 'from .env'})",
    )
    parser.add_argument(
        "--judge",
        default=DEFAULT_JUDGE_MODEL,
        help=f"Judge model identifier (default: {DEFAULT_JUDGE_MODEL})",
    )
    parser.add_argument(
        "--parallel", "-p",
        type=int,
        default=DEFAULT_PARALLEL,
        metavar="N",
        help=f"Number of parallel containers (default: {DEFAULT_PARALLEL})",
    )
    parser.add_argument(
        "--timeout-multiplier",
        type=float,
        default=TIMEOUT_MULTIPLIER,
        help=f"Scale all task timeouts (default: {TIMEOUT_MULTIPLIER})",
    )
    parser.add_argument(
        "--runs",
        type=int,
        default=BENCHMARK_RUNS,
        help=f"Number of runs per task (default: {BENCHMARK_RUNS})",
    )
    parser.add_argument(
        "--resume",
        action="store_true",
        help="Resume from last interrupted run (skip completed tasks)",
    )
    parser.add_argument(
        "--verbose", "-v",
        action="store_true",
        help="Enable verbose logging",
    )

    args = parser.parse_args()

    if not args.model:
        logger.error("--model is required (or set DEFAULT_MODEL in .env)")
        sys.exit(1)

    # Validate model
    if not _validate_openrouter_model(args.model):
        sys.exit(1)

    logger.info("DataClaw — per-task container benchmark")
    logger.info("Model: %s | Judge: %s | Parallel: %d", args.model, args.judge, args.parallel)

    # Load tasks
    if args.task:
        task_file = Path(args.task)
        if not task_file.exists():
            logger.error("File not found: %s", task_file)
            sys.exit(1)
        loader = TaskLoader(task_file.parent)
        task = loader.load_task(task_file)
        tasks = [task]
        suite_label = task.task_id
    else:
        if not TASKS_DIR.exists():
            logger.error("Tasks directory not found: %s", TASKS_DIR)
            sys.exit(1)
        loader = TaskLoader(TASKS_DIR)
        all_tasks = loader.load_all_tasks()
        if args.suite == "all":
            tasks = all_tasks
            suite_label = "all"
        else:
            requested = {tid.strip() for tid in args.suite.split(",") if tid.strip()}
            known = {t.task_id for t in all_tasks}
            unknown = requested - known
            if unknown:
                logger.error("Unknown task IDs: %s", ", ".join(sorted(unknown)))
                sys.exit(1)
            tasks = [t for t in all_tasks if t.task_id in requested]
            suite_label = args.suite

    if not tasks:
        logger.error("No tasks to run")
        sys.exit(1)

    logger.info("Tasks: %d | Runs per task: %d", len(tasks), args.runs)

    # Execute
    all_results: List[Dict[str, Any]] = []
    model_slug = _slugify_model(args.model)
    prog_path = _progress_path(model_slug)

    # --resume: load previous progress and validate parameters
    completed_keys: set = set()
    progress_entries: List[Dict[str, Any]] = []

    if args.resume:
        prog_data = _load_progress(prog_path)
        if prog_data is not None:
            mismatches = []
            if prog_data.get("model") != args.model:
                mismatches.append(
                    f"model (progress={prog_data.get('model')}, current={args.model})"
                )
            if prog_data.get("suite") != suite_label:
                mismatches.append(
                    f"suite (progress={prog_data.get('suite')}, current={suite_label})"
                )
            if prog_data.get("runs") != args.runs:
                mismatches.append(
                    f"runs (progress={prog_data.get('runs')}, current={args.runs})"
                )
            if mismatches:
                logger.error(
                    "Resume failed: parameter mismatch — %s. "
                    "Use the same parameters as the original run, or remove %s to start fresh.",
                    "; ".join(mismatches), prog_path,
                )
                sys.exit(1)

            for entry in prog_data.get("completed", []):
                completed_keys.add((entry["task_id"], entry["run_index"]))
                all_results.append(entry["result"])
                progress_entries.append(entry)
            logger.info("Resuming: %d tasks already completed", len(completed_keys))
        else:
            logger.warning(
                "No progress file found for model '%s', starting from scratch",
                args.model,
            )

    for run_index in range(args.runs):
        if args.runs > 1:
            logger.info("=== Run %d/%d ===", run_index + 1, args.runs)

        pending_tasks = [
            t for t in tasks if (t.task_id, run_index) not in completed_keys
        ]
        if not pending_tasks:
            logger.info("All tasks in run %d already completed, skipping", run_index + 1)
            continue
        if completed_keys:
            skipped = len(tasks) - len(pending_tasks)
            if skipped:
                logger.info(
                    "Skipping %d completed task(s), running %d remaining",
                    skipped, len(pending_tasks),
                )

        if args.parallel <= 1:
            for i, task in enumerate(pending_tasks, 1):
                logger.info("--- Task %d/%d: %s ---", i, len(pending_tasks), task.task_id)
                result = run_single_task(
                    task, args.model, args.judge, args.timeout_multiplier,
                )
                all_results.append(result)
                if result.get("error"):
                    logger.warning(
                        "[%s] Skipping progress save (error: %s) — will retry on resume",
                        task.task_id, result["error"],
                    )
                else:
                    with _progress_lock:
                        progress_entries.append({
                            "task_id": task.task_id,
                            "run_index": run_index,
                            "result": result,
                        })
                        _save_progress(
                            prog_path, args.model, suite_label, args.runs,
                            progress_entries,
                        )
        else:
            with ThreadPoolExecutor(max_workers=args.parallel) as pool:
                futures = {
                    pool.submit(
                        run_single_task,
                        task, args.model, args.judge, args.timeout_multiplier,
                    ): (task.task_id, run_index)
                    for task in pending_tasks
                }
                for future in as_completed(futures):
                    tid, ridx = futures[future]
                    try:
                        result = future.result()
                    except Exception as exc:
                        logger.error("[%s] Thread exception: %s", tid, exc)
                        result = {
                            "task_id": tid,
                            "model": args.model,
                            "scores": {},
                            "grade": None,
                            "process_grade": None,
                            "usage": {},
                            "error": str(exc),
                            "elapsed_time": 0.0,
                        }
                    all_results.append(result)
                    if result.get("error"):
                        logger.warning(
                            "[%s] Skipping progress save (error: %s) — will retry on resume",
                            tid, result["error"],
                        )
                    else:
                        with _progress_lock:
                            progress_entries.append({
                                "task_id": tid,
                                "run_index": ridx,
                                "result": result,
                            })
                            _save_progress(
                                prog_path, args.model, suite_label, args.runs,
                                progress_entries,
                            )

    # Summary
    _print_summary(all_results, args.model)
    summary_path = _write_global_summary(
        all_results, args.model, model_slug, suite_label, args.runs,
        task_objects=tasks,
    )
    logger.info("Summary written to: %s", summary_path)

    # Clean up progress file only if all tasks succeeded (no errors).
    # If there are errors, keep progress so --resume can retry just the
    # errored tasks without re-running the ones that already succeeded.
    error_count = sum(1 for r in all_results if r.get("error"))
    if error_count == 0:
        try:
            prog_path.unlink(missing_ok=True)
        except OSError:
            pass
    else:
        logger.info(
            "%d task(s) had errors — progress file kept for --resume",
            error_count,
        )


if __name__ == "__main__":
    main()