|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[1] |
| 12 | +if str(ROOT) not in sys.path: |
| 13 | + sys.path.insert(0, str(ROOT)) |
| 14 | + |
| 15 | +from loopx.benchmark_adapters.edgebench import ( # noqa: E402 |
| 16 | + EDGEBENCH_OFFICIAL_TIMEOUT_SECONDS, |
| 17 | + build_edgebench_sforge_preflight, |
| 18 | + build_edgebench_sforge_run_plan, |
| 19 | + reduce_edgebench_final_result, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def assert_boundary(payload: dict[str, object]) -> None: |
| 24 | + boundary = payload["boundary"] |
| 25 | + assert isinstance(boundary, dict) |
| 26 | + assert boundary["container_started"] is False |
| 27 | + assert boundary["task_body_read"] is False |
| 28 | + assert boundary["hidden_tests_read"] is False |
| 29 | + assert boundary["model_api_invoked"] is False |
| 30 | + assert boundary["raw_logs_read"] is False |
| 31 | + assert boundary["raw_trajectory_read"] is False |
| 32 | + assert boundary["credential_values_recorded"] is False |
| 33 | + assert boundary["local_paths_recorded"] is False |
| 34 | + assert boundary["command_argv_recorded"] is False |
| 35 | + assert boundary["upload_invoked"] is False |
| 36 | + assert boundary["submit_invoked"] is False |
| 37 | + assert boundary["leaderboard_evidence"] is False |
| 38 | + |
| 39 | + |
| 40 | +def ready_preflight() -> dict[str, object]: |
| 41 | + return build_edgebench_sforge_preflight( |
| 42 | + task_id="ad_placement_optimization", |
| 43 | + backend="docker", |
| 44 | + linux_host=True, |
| 45 | + sforge_available=True, |
| 46 | + container_runtime_available=True, |
| 47 | + task_catalog_ready=True, |
| 48 | + task_images_ready=True, |
| 49 | + judge_ready=True, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def test_contracts() -> None: |
| 54 | + ready = ready_preflight() |
| 55 | + assert ready["ready"] is True |
| 56 | + assert ready["open_task_count"] == 51 |
| 57 | + assert ready["first_blocker"] == "ready_for_edgebench_run_plan" |
| 58 | + assert ready["score_policy"]["selection"] == "best_submission_over_time" |
| 59 | + assert_boundary(ready) |
| 60 | + |
| 61 | + blocked = build_edgebench_sforge_preflight( |
| 62 | + task_id="ad_placement_optimization", |
| 63 | + backend="docker", |
| 64 | + linux_host=True, |
| 65 | + sforge_available=False, |
| 66 | + container_runtime_available=False, |
| 67 | + task_catalog_ready=False, |
| 68 | + task_images_ready=False, |
| 69 | + judge_ready=False, |
| 70 | + ) |
| 71 | + assert blocked["ready"] is False |
| 72 | + assert blocked["first_blocker"] == "sforge_not_available" |
| 73 | + assert_boundary(blocked) |
| 74 | + |
| 75 | + plan = build_edgebench_sforge_run_plan( |
| 76 | + preflight=ready, |
| 77 | + agent="codex", |
| 78 | + model="gpt-5.5", |
| 79 | + timeout_seconds=7_200, |
| 80 | + run_id="edgebench-smoke-001", |
| 81 | + ) |
| 82 | + assert plan["ready"] is True |
| 83 | + assert plan["runner"]["entrypoint"] == "sforge run" |
| 84 | + assert plan["runner"]["required_secret_names"] == ["SFORGE_AGENT_API_KEY"] |
| 85 | + assert plan["evaluation"]["official_setting"] is False |
| 86 | + assert plan["evaluation"]["leaderboard_eligible"] is False |
| 87 | + assert_boundary(plan) |
| 88 | + |
| 89 | + forged = build_edgebench_sforge_run_plan( |
| 90 | + preflight={ |
| 91 | + "ready": True, |
| 92 | + "task_id": "ad_placement_optimization", |
| 93 | + "backend": "docker", |
| 94 | + }, |
| 95 | + agent="codex", |
| 96 | + model="gpt-5.5", |
| 97 | + timeout_seconds=7_200, |
| 98 | + run_id="edgebench-smoke-forged", |
| 99 | + ) |
| 100 | + assert forged["ready"] is False |
| 101 | + assert forged["first_blocker"] == "edgebench_preflight_contract_invalid" |
| 102 | + assert_boundary(forged) |
| 103 | + |
| 104 | + try: |
| 105 | + build_edgebench_sforge_run_plan( |
| 106 | + preflight=ready, |
| 107 | + agent="codex", |
| 108 | + model="gpt-5.5", |
| 109 | + timeout_seconds=EDGEBENCH_OFFICIAL_TIMEOUT_SECONDS + 1, |
| 110 | + run_id="edgebench-smoke-002", |
| 111 | + ) |
| 112 | + except ValueError as exc: |
| 113 | + assert "timeout_seconds" in str(exc) |
| 114 | + else: |
| 115 | + raise AssertionError("run plan accepted a budget above 12 hours") |
| 116 | + |
| 117 | + compact = reduce_edgebench_final_result( |
| 118 | + task_id="ad_placement_optimization", |
| 119 | + run_id="edgebench-smoke-001", |
| 120 | + result={ |
| 121 | + "best_score": 62.9, |
| 122 | + "best_pass_rate": 0.8, |
| 123 | + "best_round": "agent-4", |
| 124 | + "total_rounds": 7, |
| 125 | + "agent_submissions": 4, |
| 126 | + "auto_submissions": 3, |
| 127 | + "runtime_seconds": 7_199.5, |
| 128 | + "resume_count": 1, |
| 129 | + "timed_out": True, |
| 130 | + "agent_output": "must not be projected", |
| 131 | + "archive_size_bytes": 1234, |
| 132 | + }, |
| 133 | + ) |
| 134 | + assert compact["countable"] is True |
| 135 | + assert compact["metrics"] == { |
| 136 | + "best_score": 62.9, |
| 137 | + "best_pass_rate": 0.8, |
| 138 | + "best_round": "agent-4", |
| 139 | + "total_rounds": 7, |
| 140 | + "agent_submissions": 4, |
| 141 | + "auto_submissions": 3, |
| 142 | + "runtime_seconds": 7_199.5, |
| 143 | + "resume_count": 1, |
| 144 | + "timed_out": True, |
| 145 | + } |
| 146 | + assert "agent_output" not in json.dumps(compact) |
| 147 | + assert "archive_size_bytes" not in json.dumps(compact) |
| 148 | + assert_boundary(compact) |
| 149 | + |
| 150 | + mismatched = reduce_edgebench_final_result( |
| 151 | + task_id="ad_placement_optimization", |
| 152 | + run_id="edgebench-smoke-mismatch", |
| 153 | + result={ |
| 154 | + "best_score": 62.9, |
| 155 | + "total_rounds": 7, |
| 156 | + "agent_submissions": 4, |
| 157 | + "auto_submissions": 2, |
| 158 | + "runtime_seconds": 7_199.5, |
| 159 | + }, |
| 160 | + ) |
| 161 | + assert mismatched["countable"] is False |
| 162 | + assert mismatched["first_blocker"] == "edgebench_submission_rounds_mismatch" |
| 163 | + assert_boundary(mismatched) |
| 164 | + |
| 165 | + |
| 166 | +def run_cli(*args: str) -> subprocess.CompletedProcess[str]: |
| 167 | + return subprocess.run( |
| 168 | + [sys.executable, "-m", "loopx.cli", "--format", "json", *args], |
| 169 | + cwd=ROOT, |
| 170 | + text=True, |
| 171 | + capture_output=True, |
| 172 | + check=False, |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | +def test_cli() -> None: |
| 177 | + preflight = run_cli( |
| 178 | + "benchmark", |
| 179 | + "edgebench-preflight", |
| 180 | + "--task-id", |
| 181 | + "ad_placement_optimization", |
| 182 | + "--no-environment-probe", |
| 183 | + ) |
| 184 | + assert preflight.returncode == 0, preflight.stderr |
| 185 | + blocked = json.loads(preflight.stdout) |
| 186 | + assert blocked["ok"] is True |
| 187 | + assert blocked["ready"] is False |
| 188 | + assert blocked["first_blocker"] == "edgebench_linux_host_required" |
| 189 | + assert_boundary(blocked) |
| 190 | + |
| 191 | + required = run_cli( |
| 192 | + "benchmark", |
| 193 | + "edgebench-preflight", |
| 194 | + "--task-id", |
| 195 | + "ad_placement_optimization", |
| 196 | + "--no-environment-probe", |
| 197 | + "--require-ready", |
| 198 | + ) |
| 199 | + assert required.returncode == 1 |
| 200 | + assert json.loads(required.stdout)["error"] == "edgebench_linux_host_required" |
| 201 | + |
| 202 | + with tempfile.TemporaryDirectory(prefix="loopx-edgebench-smoke-") as temp_dir: |
| 203 | + root = Path(temp_dir) |
| 204 | + preflight_path = root / "preflight.json" |
| 205 | + preflight_path.write_text(json.dumps(ready_preflight()), encoding="utf-8") |
| 206 | + plan = run_cli( |
| 207 | + "benchmark", |
| 208 | + "edgebench-run-plan", |
| 209 | + "--preflight-json", |
| 210 | + str(preflight_path), |
| 211 | + "--agent", |
| 212 | + "codex", |
| 213 | + "--model", |
| 214 | + "gpt-5.5", |
| 215 | + "--run-id", |
| 216 | + "edgebench-smoke-001", |
| 217 | + "--timeout-seconds", |
| 218 | + "7200", |
| 219 | + "--require-ready", |
| 220 | + ) |
| 221 | + assert plan.returncode == 0, plan.stderr |
| 222 | + plan_payload = json.loads(plan.stdout) |
| 223 | + assert plan_payload["ok"] is True |
| 224 | + assert plan_payload["ready"] is True |
| 225 | + assert_boundary(plan_payload) |
| 226 | + |
| 227 | + result_path = root / "final_result.json" |
| 228 | + result_path.write_text( |
| 229 | + json.dumps( |
| 230 | + { |
| 231 | + "best_score": 62.9, |
| 232 | + "best_pass_rate": 0.8, |
| 233 | + "best_round": "agent-4", |
| 234 | + "total_rounds": 7, |
| 235 | + "agent_submissions": 4, |
| 236 | + "auto_submissions": 3, |
| 237 | + "runtime_seconds": 7199.5, |
| 238 | + "resume_count": 1, |
| 239 | + "timed_out": True, |
| 240 | + } |
| 241 | + ), |
| 242 | + encoding="utf-8", |
| 243 | + ) |
| 244 | + reduced = run_cli( |
| 245 | + "benchmark", |
| 246 | + "edgebench-result-reduce", |
| 247 | + "--result-json", |
| 248 | + str(result_path), |
| 249 | + "--task-id", |
| 250 | + "ad_placement_optimization", |
| 251 | + "--run-id", |
| 252 | + "edgebench-smoke-001", |
| 253 | + "--require-countable", |
| 254 | + ) |
| 255 | + assert reduced.returncode == 0, reduced.stderr |
| 256 | + reduced_payload = json.loads(reduced.stdout) |
| 257 | + assert reduced_payload["ok"] is True |
| 258 | + assert reduced_payload["countable"] is True |
| 259 | + assert_boundary(reduced_payload) |
| 260 | + |
| 261 | + |
| 262 | +def main() -> int: |
| 263 | + test_contracts() |
| 264 | + test_cli() |
| 265 | + print("edgebench-benchmark-adapter-smoke ok") |
| 266 | + return 0 |
| 267 | + |
| 268 | + |
| 269 | +if __name__ == "__main__": |
| 270 | + raise SystemExit(main()) |
0 commit comments