-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCodex_CLI_run_benchmark.py
More file actions
1346 lines (1156 loc) · 44.8 KB
/
Codex_CLI_run_benchmark.py
File metadata and controls
1346 lines (1156 loc) · 44.8 KB
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
#!/usr/bin/env python3
"""
Codex_CLI_run_benchmark.py — Batch Benchmark Runner for OpenAI Codex CLI
Automates running OpenAI Codex CLI (`codex exec`) against CocoaBench tasks.
Key mechanism:
- `codex exec` runs Codex non-interactively (no TUI, no user prompts)
- `--dangerously-bypass-approvals-and-sandbox` (--yolo) ensures ZERO
interruptions — no approval dialogs, no sandbox permission prompts
- `--output-last-message` captures the final assistant text to a file
- `--json` streams JSONL events for progress monitoring
- `--skip-git-repo-check` allows running in non-git temp directories
- `--ephemeral` avoids persisting session state to disk
Workflow per task:
1. Copy task folder to an isolated temp workspace, EXCLUDING solution.md,
test.py, evaluation.md, __pycache__ (prevents answer leakage)
2. Optionally create an AGENTS.md in the workspace for persistent guidance
3. Invoke `codex exec` with the task prompt via stdin
4. Capture the final message and JSONL conversation events
5. Evaluate using task's test.py (primary) or solution.md (fallback)
6. Log results to JSON
Codex JSONL event format (codex exec --json):
- thread.started — session begins, has thread_id
- turn.started — a new agent turn begins
- item.started — an item begins (command_execution shows command)
- item.completed — an item finishes with full content:
- type=agent_message — assistant text in "text" field
- type=command_execution — bash cmd in "command", output in
"aggregated_output", "exit_code"
- turn.completed — turn finishes, has "usage" with token counts
Usage:
python Codex_CLI_run_benchmark.py [OPTIONS]
Requirements:
- Python 3.10+
- Codex CLI installed (`npm i -g @openai/codex`) and authenticated
Examples:
# Quick test with 2 tasks
python Codex_CLI_run_benchmark.py --limit 2 --verbose
# Full run with specific model
python Codex_CLI_run_benchmark.py --model gpt-5.4
# Run specific tasks in parallel
python Codex_CLI_run_benchmark.py --tasks arrow-hunt nonogram-2 --workers 4
# Resume after interruption
python Codex_CLI_run_benchmark.py --skip-existing
# Dump raw JSONL events for debugging event parsing
python Codex_CLI_run_benchmark.py --tasks some-task --verbose --dump-events
Repository layout expected:
cocoa-agent/
├── Codex_CLI_run_benchmark.py <- this script
├── cocoabench-head/
│ ├── some-task/
│ │ ├── task.yaml
│ │ ├── test.py <- evaluation script (primary)
│ │ ├── solution.md <- expected answer (fallback)
│ │ └── ...
│ └── ...
└── benchmark_results_codex/ <- auto-created output
"""
import subprocess
import json
import os
import re
import sys
import tempfile
import shutil
import time
import argparse
import threading
import importlib
import importlib.util
from pathlib import Path
from datetime import datetime
from typing import Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
# =============================================================================
# Configuration Constants
# =============================================================================
DEFAULT_BENCH_DIR = "cocoabench-head"
DEFAULT_OUTPUT_DIR = "benchmark_results_codex"
DEFAULT_TIMEOUT_SEC = 900 # 15 minutes per task
DEFAULT_WORKERS = 1 # sequential by default
SOLUTION_FILENAME = "solution.md"
TASK_FILENAME = "task.yaml"
# Files/dirs excluded from agent workspace to prevent answer leakage.
WORKSPACE_EXCLUDE = [SOLUTION_FILENAME, "test.py", "evaluation.md", "__pycache__"]
# Approximate pricing for codex-mini-latest (USD per 1M tokens).
# Adjust these if using a different model.
COST_PER_M_INPUT = 1.50 # non-cached input tokens
COST_PER_M_CACHED_INPUT = 0.375 # cached input tokens
COST_PER_M_OUTPUT = 6.00 # output tokens
# AGENTS.md content placed in each workspace to guide Codex behavior.
AGENTS_MD_CONTENT = """\
# Benchmark Task Instructions
## Rules
- Read task.yaml for the full task description.
- Solve the task step by step. You may write and execute code as needed.
- When you have your final answer, output it using EXACTLY this XML format:
<answer>YOUR_ANSWER_HERE</answer>
- Do NOT search for, read, or attempt to access any file named
`solution.md`, `test.py`, or `evaluation.md`. They do not exist in
your workspace.
## Workflow
1. Read task.yaml
2. Examine any other files in the working directory (data, images, code, etc.)
3. Reason through the problem; run code if helpful
4. Output your final answer inside <answer> tags
"""
# =============================================================================
# Task Discovery
# =============================================================================
def find_tasks(bench_dir: Path) -> list[Path]:
"""
Scan the benchmark directory and return a sorted list of task folders.
A valid task folder must contain task.yaml.
"""
if not bench_dir.exists():
print(f" ERROR: Benchmark directory '{bench_dir}' not found.")
print(f" Make sure you run this script from the repo root.")
sys.exit(1)
tasks = []
for entry in sorted(bench_dir.iterdir()):
if entry.is_dir() and (entry / TASK_FILENAME).exists():
tasks.append(entry)
return tasks
# =============================================================================
# test.py Evaluation (PRIMARY METHOD)
# =============================================================================
def evaluate_with_test_py(
task_dir: Path,
raw_result: str,
status: str,
conversation: list,
instruction: str,
num_turns: int,
) -> Optional[dict]:
"""
Load the task's test.py and call its test(result) function.
Returns dict with {passed, feedback, details} or None if test.py
is unavailable.
"""
test_py_path = task_dir / "test.py"
if not test_py_path.exists():
return None
module_name = f"task_test_{task_dir.name.replace('-', '_').replace('.', '_')}"
if module_name in sys.modules:
del sys.modules[module_name]
spec = importlib.util.spec_from_file_location(module_name, str(test_py_path))
if spec is None or spec.loader is None:
return {
"passed": False,
"feedback": f"Failed to create module spec for {test_py_path}",
"details": {"error": "spec_creation_failed"},
}
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except Exception as exc:
return {
"passed": False,
"feedback": f"Failed to load test.py: {exc}",
"details": {"error": str(exc)},
}
test_func = getattr(module, "test", None)
if test_func is None:
return {
"passed": False,
"feedback": "test.py does not define a test() function",
"details": {"error": "no_test_function"},
}
result_dict = {
"task_name": task_dir.name,
"task_result": raw_result,
"conversation": conversation,
"execution_trace": [],
"status": status,
"instruction": instruction,
"iterations": num_turns or 0,
"sandbox": {},
}
try:
test_output = test_func(result_dict)
if not isinstance(test_output, dict):
return {
"passed": False,
"feedback": f"test() returned non-dict: {type(test_output)}",
"details": {"error": "invalid_return_type"},
}
return test_output
except Exception as exc:
return {
"passed": False,
"feedback": f"test() raised an exception: {exc}",
"details": {"error": str(exc)},
}
# =============================================================================
# Expected Answer Extraction (FALLBACK — from solution.md)
# =============================================================================
def extract_expected_answer(task_dir: Path) -> Optional[str]:
"""
Read solution.md and extract the expected answer.
Used as fallback when test.py is not available.
"""
sol_path = task_dir / SOLUTION_FILENAME
if not sol_path.exists():
return None
text = sol_path.read_text(encoding="utf-8", errors="replace")
# Priority 1: <answer>...</answer>
m = re.search(r"<answer>(.*?)</answer>", text, re.DOTALL | re.IGNORECASE)
if m:
return m.group(1).strip()
# Priority 2: "ANSWER: value"
m = re.search(r"^\s*ANSWER\s*:\s*(.+)$", text, re.MULTILINE | re.IGNORECASE)
if m:
return m.group(1).strip()
# Priority 3a: **Expected Answer[...]:** value (same line)
m = re.search(
r"\*\*Expected\s+Answer[^*]*\*\*\s*:?\s*(.+)", text, re.IGNORECASE
)
if m:
val = re.sub(r"^\*\*(.+)\*\*$", r"\1", m.group(1).strip())
if val:
return val
# Priority 3b: **Expected Answer[...]:** with value on next line(s)
m = re.search(
r"\*\*Expected\s+Answer[^*]*\*\*\s*:?\s*\n\s*(.+?)(?:\n\s*\n|\Z)",
text, re.DOTALL | re.IGNORECASE,
)
if m:
val = re.sub(r"^\*\*(.+)\*\*$", r"\1", m.group(1).strip())
if val:
return val
# Priority 4: ### Final Answer heading
m = re.search(
r"#{1,6}\s*Final\s+Answer\b[^\n]*\n(.*?)(?=\n\s*#{1,6}\s|\Z)",
text, re.DOTALL | re.IGNORECASE,
)
if m and m.group(1).strip():
return m.group(1).strip()
# Priority 5: ## Answer heading
m = re.search(
r"#{1,6}\s*Answer\s*\n(.*?)(?=\n\s*#{1,6}\s|\Z)",
text, re.DOTALL | re.IGNORECASE,
)
if m and m.group(1).strip():
return m.group(1).strip()
# Priority 6: Bold label + bold value on next line
bold_pairs = re.findall(r"\*\*[^*]+\*\*\s*\n\s*\*\*([^*]+)\*\*", text)
if bold_pairs:
return bold_pairs[-1].strip()
# Priority 7: "Final Answer:" inline
m = re.search(
r"Final\s+Answer\s*[:\-]\s*(.+?)(?:\n\s*\n|\Z)",
text, re.DOTALL | re.IGNORECASE,
)
if m and m.group(1).strip():
return m.group(1).strip()
return None
# =============================================================================
# Workspace Isolation
# =============================================================================
def create_workspace(task_dir: Path, tmp_root: str, git_init: bool = False) -> Path:
"""
Copy the task folder into a temp directory, EXCLUDING answer files.
Optionally initialize a git repo for better Codex compatibility.
Also creates an AGENTS.md to guide Codex behavior.
"""
dest = Path(tmp_root) / task_dir.name
shutil.copytree(
task_dir, dest,
ignore=shutil.ignore_patterns(*WORKSPACE_EXCLUDE),
)
# Create AGENTS.md for persistent agent guidance
agents_path = dest / "AGENTS.md"
agents_path.write_text(AGENTS_MD_CONTENT, encoding="utf-8")
if git_init:
env = {
**os.environ,
"GIT_AUTHOR_NAME": "benchmark",
"GIT_AUTHOR_EMAIL": "bench@test.local",
"GIT_COMMITTER_NAME": "benchmark",
"GIT_COMMITTER_EMAIL": "bench@test.local",
}
subprocess.run(
["git", "init"], cwd=str(dest),
capture_output=True, env=env, timeout=10,
)
subprocess.run(
["git", "add", "."], cwd=str(dest),
capture_output=True, env=env, timeout=10,
)
subprocess.run(
["git", "commit", "-m", "init benchmark workspace"], cwd=str(dest),
capture_output=True, env=env, timeout=10,
)
return dest
# =============================================================================
# Prompt Building
# =============================================================================
def build_prompt(task_yaml_content: str) -> str:
"""
Construct the prompt sent to Codex via `codex exec`.
"""
return (
"You are solving a benchmark task. "
"The current working directory contains the task files.\n\n"
"Instructions:\n"
"1. Read the task description below carefully.\n"
"2. List and examine any other files in the current directory that may "
"be relevant (data files, images, code, CSVs, etc.).\n"
"3. Solve the task step by step. Write and run code if needed.\n"
"4. When you have your final answer, you MUST output it using EXACTLY "
"this format:\n"
" <answer>YOUR_ANSWER_HERE</answer>\n\n"
"CRITICAL RULE: Do NOT search for, read, or access any file named "
"'solution.md', 'test.py', or 'evaluation.md'. "
"They do not exist in your workspace.\n\n"
"--- TASK DESCRIPTION (from task.yaml) ---\n"
f"{task_yaml_content}\n"
"--- END TASK DESCRIPTION ---\n"
)
# =============================================================================
# Answer Extraction
# =============================================================================
def extract_answer(text: str) -> Optional[str]:
"""
Extract the answer from Codex's response text.
Looks for <answer> tags first, falls back to last non-empty line.
"""
if not text:
return None
m = re.search(r"<answer>(.*?)</answer>", text, re.DOTALL | re.IGNORECASE)
if m:
return m.group(1).strip()
lines = [ln.strip() for ln in text.strip().splitlines() if ln.strip()]
if lines:
return lines[-1]
return None
# =============================================================================
# Answer Comparison (FALLBACK)
# =============================================================================
def normalize(s: str) -> str:
s = s.strip().lower()
s = s.rstrip(".,;:!?")
s = re.sub(r"\s+", " ", s)
return s
def compare_answers(
agent_answer: Optional[str], expected: Optional[str]
) -> Optional[bool]:
if agent_answer is None or expected is None:
return None
a, b = normalize(agent_answer), normalize(expected)
if not a or not b:
return None
if a == b:
return True
try:
if abs(float(a) - float(b)) < 1e-9:
return True
except (ValueError, OverflowError):
pass
if len(b) <= 60 and b in a:
return True
if len(a) <= 60 and a in b:
return True
return False
# =============================================================================
# Codex Event Helpers
# =============================================================================
_BASH_PREFIX = "/bin/bash -lc "
def _extract_inner_command(full_cmd: str) -> str:
"""
Extract the user-facing command from Codex's /bin/bash -lc wrapper.
Examples:
'/bin/bash -lc pwd' → 'pwd'
'/bin/bash -lc "sed -n \'1,200p\' f.txt"' → "sed -n '1,200p' f.txt"
'/bin/bash -lc \'rg --files\'' → 'rg --files'
"""
if not full_cmd.startswith(_BASH_PREFIX):
return full_cmd
inner = full_cmd[len(_BASH_PREFIX):]
# Strip one layer of outer quotes if present
if len(inner) >= 2:
if (inner[0] == '"' and inner[-1] == '"') or \
(inner[0] == "'" and inner[-1] == "'"):
inner = inner[1:-1]
return inner
def _estimate_cost(
input_tokens: int,
cached_input_tokens: int,
output_tokens: int,
) -> float:
"""
Estimate USD cost from token counts using module-level pricing constants.
"""
new_input = max(input_tokens - cached_input_tokens, 0)
cost = (
new_input * COST_PER_M_INPUT / 1_000_000
+ cached_input_tokens * COST_PER_M_CACHED_INPUT / 1_000_000
+ output_tokens * COST_PER_M_OUTPUT / 1_000_000
)
return cost
# =============================================================================
# Run a Single Task via `codex exec`
# =============================================================================
def run_single_task(
task_dir: Path,
workspace: Path,
model: Optional[str] = None,
timeout_sec: int = DEFAULT_TIMEOUT_SEC,
verbose: bool = False,
dump_events: bool = False,
git_init: bool = False,
) -> dict:
"""
Invoke `codex exec` on a single task in the isolated workspace.
Returns a dict with: answer, raw_result, conversation, elapsed,
exit_code, num_turns, usage, estimated_cost, full_events, stderr, error
"""
task_text = (workspace / TASK_FILENAME).read_text(
encoding="utf-8", errors="replace"
)
prompt = build_prompt(task_text)
output_file = workspace / "_codex_last_message.txt"
# ── Build the codex exec command ──
cmd = ["codex", "exec"]
if model:
cmd += ["--model", model]
cmd += [
"--json",
"--output-last-message", str(output_file),
"--dangerously-bypass-approvals-and-sandbox",
"--cd", str(workspace),
"--ephemeral",
"--color", "never",
]
# If workspace is not a git repo, skip the check
if not git_init:
cmd.append("--skip-git-repo-check")
# Use "-" as prompt placeholder to read from stdin
cmd.append("-")
if verbose:
safe_cmd = " ".join(
c if not c.startswith("You are") else "'<prompt>'" for c in cmd
)
print(f"\n [CMD] {safe_cmd}")
print(f" [CWD] {workspace}")
t0 = time.time()
stdout_chunks: list[str] = []
stderr_chunks: list[str] = []
conversation: list[dict] = []
all_events: list[dict] = []
last_result_text = ""
num_turns = 0
total_input_tokens = 0
total_cached_input_tokens = 0
total_output_tokens = 0
proc = None
try:
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
# ── Write prompt to stdin and close ──
try:
proc.stdin.write(prompt)
proc.stdin.close()
except BrokenPipeError:
pass # Process may have exited early
# ── Read stderr in background thread ──
def read_stderr():
try:
for line in proc.stderr:
stderr_chunks.append(line)
except Exception:
pass
stderr_thread = threading.Thread(target=read_stderr, daemon=True)
stderr_thread.start()
# ── Stream and parse JSONL events from stdout ──
deadline = t0 + timeout_sec
for line in proc.stdout:
stdout_chunks.append(line)
now = time.time()
line_stripped = line.strip()
if not line_stripped:
continue
try:
event = json.loads(line_stripped)
except json.JSONDecodeError:
if verbose:
print(f" [RAW] {line_stripped[:200]}")
continue
all_events.append(event)
event_type = event.get("type", "")
# ── Dump raw event JSON if requested ──
if dump_events:
compact = json.dumps(event, ensure_ascii=False)
if len(compact) > 500:
compact = compact[:500] + "..."
print(f" [DUMP] {compact}")
# ==============================================================
# Parse Codex CLI JSONL events
# ==============================================================
# ── item.started: show tool calls as they begin ──
if event_type == "item.started":
item = event.get("item", {})
if isinstance(item, dict):
itype = item.get("type", "")
if itype == "command_execution":
raw_cmd = item.get("command", "")
inner_cmd = _extract_inner_command(raw_cmd)
if verbose and not dump_events:
display = inner_cmd[:120]
if len(inner_cmd) > 120:
display += "..."
print(
f" [TOOL] Bash: "
f"{{'command': '{display}'}}"
)
# ── item.completed: record content ──
elif event_type == "item.completed":
item = event.get("item", {})
if not isinstance(item, dict):
continue
itype = item.get("type", "")
# ── Agent message (assistant text) ──
if itype == "agent_message":
text = item.get("text", "")
if text:
last_result_text = text
conversation.append({
"role": "assistant",
"content": text,
})
if verbose and not dump_events:
preview = text.replace("\n", " ")[:300]
print(f" [CODEX] {preview}")
# ── Command execution (tool call + result) ──
elif itype == "command_execution":
raw_cmd = item.get("command", "")
output = item.get("aggregated_output", "")
inner_cmd = _extract_inner_command(raw_cmd)
# Record the tool call
conversation.append({
"role": "assistant",
"content": "",
"tool_calls": [{
"id": item.get("id", ""),
"type": "function",
"function": {
"name": "Bash",
"arguments": json.dumps(
{"command": inner_cmd},
ensure_ascii=False,
),
},
}],
})
# Record the tool output
conversation.append({
"role": "tool",
"tool_call_id": item.get("id", ""),
"content": output,
})
# ── Unknown item type ──
else:
if verbose and not dump_events:
text = item.get("text", "")
if text:
preview = text.replace("\n", " ")[:200]
print(
f" [{itype.upper() or 'ITEM'}] "
f"{preview}"
)
# ── turn.completed: track usage ──
elif event_type == "turn.completed":
num_turns += 1
usage = event.get("usage", {})
total_input_tokens += usage.get("input_tokens", 0)
total_cached_input_tokens += usage.get(
"cached_input_tokens", 0
)
total_output_tokens += usage.get("output_tokens", 0)
if verbose and not dump_events:
total_tok = total_input_tokens + total_output_tokens
cost = _estimate_cost(
total_input_tokens,
total_cached_input_tokens,
total_output_tokens,
)
print()
print(
f" [DONE] tokens={total_tok} "
f"({total_input_tokens}in/"
f"{total_output_tokens}out), "
f"turns={num_turns}, "
f"est_cost=${cost:.4f}"
)
# ── turn.started / thread.started: silent ──
elif event_type in ("turn.started", "thread.started"):
pass
# ── Fallback for unknown event types ──
elif verbose and not dump_events:
print(f" [EVENT] type={event_type}")
# ── Check timeout ──
if now > deadline:
proc.kill()
proc.wait()
elapsed = time.time() - t0
final_text = _read_output_file(output_file, last_result_text)
answer = extract_answer(final_text)
cost = _estimate_cost(
total_input_tokens,
total_cached_input_tokens,
total_output_tokens,
)
return {
"answer": answer,
"raw_result": final_text or "(timeout, partial captured)",
"conversation": conversation,
"elapsed": round(elapsed, 2),
"exit_code": -1,
"num_turns": num_turns,
"usage": {
"input_tokens": total_input_tokens,
"cached_input_tokens": total_cached_input_tokens,
"output_tokens": total_output_tokens,
"total_tokens": (
total_input_tokens + total_output_tokens
),
},
"estimated_cost": round(cost, 6),
"full_events": all_events,
"stderr": "".join(stderr_chunks)[:2000],
"error": f"Timeout after {timeout_sec}s",
}
# ── Process finished normally ──
proc.wait()
stderr_thread.join(timeout=5)
elapsed = time.time() - t0
# ── Read the final message from output file (most reliable) ──
final_text = _read_output_file(output_file, last_result_text)
answer = extract_answer(final_text)
cost = _estimate_cost(
total_input_tokens,
total_cached_input_tokens,
total_output_tokens,
)
return {
"answer": answer,
"raw_result": final_text,
"conversation": conversation,
"elapsed": round(elapsed, 2),
"exit_code": proc.returncode,
"num_turns": num_turns,
"usage": {
"input_tokens": total_input_tokens,
"cached_input_tokens": total_cached_input_tokens,
"output_tokens": total_output_tokens,
"total_tokens": total_input_tokens + total_output_tokens,
},
"estimated_cost": round(cost, 6),
"full_events": all_events,
"stderr": "".join(stderr_chunks)[:2000],
"error": None,
}
except FileNotFoundError:
return _error_result(0, (
"'codex' command not found. "
"Install with: npm i -g @openai/codex"
))
except Exception as exc:
if proc is not None:
try:
proc.kill()
proc.wait()
except Exception:
pass
return _error_result(time.time() - t0, str(exc))
def _read_output_file(output_file: Path, fallback_text: str) -> str:
"""
Read the --output-last-message file. Falls back to the text
accumulated from JSONL streaming if the file doesn't exist.
"""
try:
if output_file.exists():
content = output_file.read_text(encoding="utf-8", errors="replace")
if content.strip():
return content.strip()
except Exception:
pass
return fallback_text
def _error_result(elapsed: float, error_msg: str) -> dict:
return {
"answer": None,
"raw_result": "",
"conversation": [],
"elapsed": round(elapsed, 2),
"exit_code": -1,
"num_turns": 0,
"usage": {
"input_tokens": 0,
"cached_input_tokens": 0,
"output_tokens": 0,
"total_tokens": 0,
},
"estimated_cost": 0.0,
"full_events": [],
"stderr": "",
"error": error_msg,
}
# =============================================================================
# Single-task wrapper (for parallel execution)
# =============================================================================
def run_and_evaluate_single(
task_dir: Path,
out_dir: Path,
model: Optional[str],
timeout_sec: int,
verbose: bool,
dump_events: bool,
git_init: bool,
) -> dict:
"""
Complete pipeline for one task: workspace → run → evaluate → save.
Each call creates and cleans up its own temp directory.
"""
name = task_dir.name
with tempfile.TemporaryDirectory(prefix=f"codex_bench_{name}_") as tmp_root:
# 1. Create isolated workspace
workspace = create_workspace(task_dir, tmp_root, git_init=git_init)
# 2. Run Codex
result = run_single_task(
task_dir=task_dir,
workspace=workspace,
model=model,
timeout_sec=timeout_sec,
verbose=verbose,
dump_events=dump_events,
git_init=git_init,
)
# 3. Evaluate — primary: test.py, fallback: solution.md
has_error = bool(result.get("error"))
exit_ok = result.get("exit_code", -1) == 0
task_status = "success" if (not has_error and exit_ok) else "failed"
task_text = (task_dir / TASK_FILENAME).read_text(
encoding="utf-8", errors="replace"
)
test_py_output = evaluate_with_test_py(
task_dir=task_dir,
raw_result=result.get("raw_result", ""),
status=task_status,
conversation=result.get("conversation", []),
instruction=task_text,
num_turns=result.get("num_turns") or 0,
)
if test_py_output is not None:
passed = test_py_output.get("passed", False)
feedback = test_py_output.get("feedback", "")
eval_details = test_py_output.get("details", {})
eval_method = "test.py"
agent_ans = eval_details.get("output_answer") or result.get("answer")
expected_ans = eval_details.get("expected_answer")
else:
expected_ans = extract_expected_answer(task_dir)
agent_ans = result.get("answer")
match = compare_answers(agent_ans, expected_ans)
passed = match
feedback = ""
eval_details = {}
eval_method = "solution.md"
# ── Normalize answers to str (test.py may return int/float) ──
if agent_ans is not None and not isinstance(agent_ans, str):
agent_ans = str(agent_ans)
if expected_ans is not None and not isinstance(expected_ans, str):
expected_ans = str(expected_ans)
# 4. Build record
record = {
"task": name,
"passed": passed,
"eval_method": eval_method,
"feedback": feedback,
"agent_answer": agent_ans,
"expected_answer": expected_ans,
"elapsed_seconds": result.get("elapsed"),
"num_turns": result.get("num_turns"),
"exit_code": result.get("exit_code"),
"error": result.get("error"),
"usage": result.get("usage"),
"estimated_cost": result.get("estimated_cost"),
"eval_details": eval_details,
"timestamp": datetime.now().isoformat(),
}
# 5. Save per-task detail file
detail = {
**record,
"raw_result": (result.get("raw_result") or "")[:5000],
"stderr": result.get("stderr", ""),
}
detail_path = out_dir / f"{name}.json"
detail_path.write_text(
json.dumps(detail, indent=2, ensure_ascii=False), encoding="utf-8"
)
return record
# =============================================================================
# Print Helpers
# =============================================================================
def status_icon(passed: Optional[bool]) -> str:
if passed is True:
return "✅"
elif passed is False:
return "❌"
return "⚠️"
def truncate(s: Optional[str], length: int = 50) -> str:
if s is None:
return "(none)"
if not isinstance(s, str):
s = str(s)
if not s:
return "(none)"
s = s.replace("\n", " ")
return s[:length] + "..." if len(s) > length else s
def _format_cost(cost: Optional[float]) -> str:
"""Format cost for display, e.g. '$0.0291'."""
if cost is None or cost == 0:
return ""
if cost < 0.01:
return f"${cost:.4f}"
return f"${cost:.3f}"
# =============================================================================
# Main
# =============================================================================
def main():
parser = argparse.ArgumentParser(
description=(
"CocoaBench Benchmark Runner — batch-run OpenAI Codex CLI "
"on benchmark tasks"
),
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""\
Examples:
python Codex_CLI_run_benchmark.py --limit 2 --verbose
python Codex_CLI_run_benchmark.py --model gpt-5.4
python Codex_CLI_run_benchmark.py --tasks task-a task-b --workers 4
python Codex_CLI_run_benchmark.py --skip-existing
python Codex_CLI_run_benchmark.py --dry-run
python Codex_CLI_run_benchmark.py --tasks some-task --verbose --dump-events
""",