|
13 | 13 | import os |
14 | 14 | import subprocess |
15 | 15 | import sys |
| 16 | +import tempfile |
| 17 | +import threading |
16 | 18 | from datetime import datetime, timezone |
17 | 19 | from pathlib import Path |
18 | 20 |
|
|
21 | 23 | from benchmarks.utils.harbor import ( |
22 | 24 | HarborCredentialMode, |
23 | 25 | check_harbor_installed as _check_harbor_installed, |
| 26 | + completed_harbor_task_ids, |
24 | 27 | convert_harbor_to_eval_output, |
25 | 28 | get_supported_task_filter_flag, |
26 | 29 | run_harbor_evaluation as _run_harbor_evaluation, |
|
33 | 36 |
|
34 | 37 | # Output filename for results |
35 | 38 | OUTPUT_FILENAME = "output.jsonl" |
| 39 | +CHECKPOINT_INTERVAL_SECONDS = 60 |
| 40 | + |
| 41 | + |
| 42 | +def _checkpoint_gcs_uri() -> str | None: |
| 43 | + bucket = os.environ.get("RESULTS_BUCKET") |
| 44 | + model_slug = os.environ.get("MODEL_SLUG") |
| 45 | + run_id = os.environ.get("GITHUB_RUN_ID") |
| 46 | + if not bucket or not model_slug or not run_id: |
| 47 | + return None |
| 48 | + return f"gs://{bucket}/terminalbench/{model_slug}/{run_id}/checkpoint.tar.gz" |
| 49 | + |
| 50 | + |
| 51 | +def restore_harbor_checkpoint(structured_output_dir: Path) -> bool: |
| 52 | + """Restore incremental Harbor results after a Kubernetes pod retry.""" |
| 53 | + checkpoint_uri = _checkpoint_gcs_uri() |
| 54 | + if checkpoint_uri is None: |
| 55 | + return False |
| 56 | + stat = subprocess.run( |
| 57 | + ["gsutil", "-q", "stat", checkpoint_uri], |
| 58 | + capture_output=True, |
| 59 | + text=True, |
| 60 | + ) |
| 61 | + if stat.returncode != 0: |
| 62 | + return False |
| 63 | + |
| 64 | + structured_output_dir.parent.mkdir(parents=True, exist_ok=True) |
| 65 | + with tempfile.TemporaryDirectory(prefix="terminalbench-checkpoint-") as tmpdir: |
| 66 | + archive = Path(tmpdir) / "checkpoint.tar.gz" |
| 67 | + subprocess.run(["gsutil", "cp", checkpoint_uri, str(archive)], check=True) |
| 68 | + subprocess.run( |
| 69 | + [ |
| 70 | + "tar", |
| 71 | + "-xzf", |
| 72 | + str(archive), |
| 73 | + "-C", |
| 74 | + str(structured_output_dir.parent), |
| 75 | + ], |
| 76 | + check=True, |
| 77 | + ) |
| 78 | + logger.info("Restored Harbor checkpoint from %s", checkpoint_uri) |
| 79 | + return True |
| 80 | + |
| 81 | + |
| 82 | +def upload_harbor_checkpoint( |
| 83 | + harbor_output_dir: Path, |
| 84 | + output_path: Path, |
| 85 | +) -> bool: |
| 86 | + """Convert and upload all complete Harbor trials seen so far.""" |
| 87 | + checkpoint_uri = _checkpoint_gcs_uri() |
| 88 | + if checkpoint_uri is None or not harbor_output_dir.exists(): |
| 89 | + return False |
| 90 | + completed_ids = completed_harbor_task_ids(harbor_output_dir) |
| 91 | + if not completed_ids: |
| 92 | + return False |
| 93 | + |
| 94 | + convert_harbor_to_eval_output( |
| 95 | + harbor_output_dir=harbor_output_dir, |
| 96 | + eval_output_path=output_path, |
| 97 | + ) |
| 98 | + with tempfile.TemporaryDirectory(prefix="terminalbench-checkpoint-") as tmpdir: |
| 99 | + archive = Path(tmpdir) / "checkpoint.tar.gz" |
| 100 | + subprocess.run( |
| 101 | + [ |
| 102 | + "tar", |
| 103 | + "-czf", |
| 104 | + str(archive), |
| 105 | + "-C", |
| 106 | + str(output_path.parent.parent), |
| 107 | + output_path.parent.name, |
| 108 | + ], |
| 109 | + check=True, |
| 110 | + ) |
| 111 | + subprocess.run( |
| 112 | + [ |
| 113 | + "gsutil", |
| 114 | + "-h", |
| 115 | + "Cache-Control:no-cache, no-store, must-revalidate", |
| 116 | + "cp", |
| 117 | + str(archive), |
| 118 | + checkpoint_uri, |
| 119 | + ], |
| 120 | + check=True, |
| 121 | + ) |
| 122 | + logger.info( |
| 123 | + "Uploaded resumable Harbor checkpoint with %d tasks to %s", |
| 124 | + len(completed_ids), |
| 125 | + checkpoint_uri, |
| 126 | + ) |
| 127 | + return True |
| 128 | + |
| 129 | + |
| 130 | +def _checkpoint_loop( |
| 131 | + stop_event: threading.Event, |
| 132 | + harbor_output_dir: Path, |
| 133 | + output_path: Path, |
| 134 | +) -> None: |
| 135 | + while not stop_event.wait(CHECKPOINT_INTERVAL_SECONDS): |
| 136 | + try: |
| 137 | + upload_harbor_checkpoint(harbor_output_dir, output_path) |
| 138 | + except Exception: |
| 139 | + logger.exception("Failed to upload Harbor checkpoint; will retry") |
36 | 140 |
|
37 | 141 |
|
38 | 142 | def check_harbor_installed() -> bool: |
@@ -193,6 +297,11 @@ def main() -> None: |
193 | 297 | ) |
194 | 298 |
|
195 | 299 | logger.info(f"Output directory: {structured_output_dir}") |
| 300 | + structured_output_path = Path(structured_output_dir) |
| 301 | + try: |
| 302 | + restore_harbor_checkpoint(structured_output_path) |
| 303 | + except Exception: |
| 304 | + logger.exception("Failed to restore Harbor checkpoint; starting without it") |
196 | 305 | os.makedirs(structured_output_dir, exist_ok=True) |
197 | 306 |
|
198 | 307 | # Save metadata |
@@ -222,20 +331,51 @@ def main() -> None: |
222 | 331 | if not args.skip_harbor: |
223 | 332 | # Run harbor evaluation |
224 | 333 | try: |
225 | | - harbor_output_dir = run_harbor_evaluation( |
226 | | - llm=llm, |
227 | | - dataset=args.dataset, |
228 | | - output_dir=structured_output_dir, |
229 | | - num_workers=args.num_workers, |
230 | | - task_ids=task_ids, |
231 | | - n_limit=args.n_limit, |
232 | | - ) |
| 334 | + harbor_output_dir = Path(structured_output_dir) / "harbor_output" |
| 335 | + if task_ids and harbor_output_dir.exists(): |
| 336 | + completed_ids = completed_harbor_task_ids(harbor_output_dir) |
| 337 | + if completed_ids: |
| 338 | + task_ids = [ |
| 339 | + task_id for task_id in task_ids if task_id not in completed_ids |
| 340 | + ] |
| 341 | + logger.info( |
| 342 | + "Restored %d completed Harbor tasks; %d selected tasks remain", |
| 343 | + len(completed_ids), |
| 344 | + len(task_ids), |
| 345 | + ) |
| 346 | + |
| 347 | + if task_ids is None or task_ids: |
| 348 | + stop_event = threading.Event() |
| 349 | + checkpoint_thread = threading.Thread( |
| 350 | + target=_checkpoint_loop, |
| 351 | + args=(stop_event, harbor_output_dir, output_path), |
| 352 | + name="terminalbench-checkpoint", |
| 353 | + daemon=True, |
| 354 | + ) |
| 355 | + checkpoint_thread.start() |
| 356 | + try: |
| 357 | + harbor_output_dir = run_harbor_evaluation( |
| 358 | + llm=llm, |
| 359 | + dataset=args.dataset, |
| 360 | + output_dir=structured_output_dir, |
| 361 | + num_workers=args.num_workers, |
| 362 | + task_ids=task_ids, |
| 363 | + n_limit=args.n_limit, |
| 364 | + ) |
| 365 | + finally: |
| 366 | + stop_event.set() |
| 367 | + checkpoint_thread.join() |
| 368 | + else: |
| 369 | + logger.info( |
| 370 | + "All selected tasks were restored; skipping Harbor execution" |
| 371 | + ) |
233 | 372 |
|
234 | 373 | # Convert harbor output to standard format |
235 | 374 | convert_harbor_to_eval_output( |
236 | 375 | harbor_output_dir=harbor_output_dir, |
237 | 376 | eval_output_path=output_path, |
238 | 377 | ) |
| 378 | + upload_harbor_checkpoint(harbor_output_dir, output_path) |
239 | 379 |
|
240 | 380 | except Exception as e: |
241 | 381 | logger.error(f"Evaluation failed: {e}") |
|
0 commit comments