File size: 9,005 Bytes
28ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edaad73
 
 
 
 
 
 
28ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edaad73
28ec988
edaad73
28ec988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import importlib.util
import json
import os
import shutil
import subprocess
import sys
from datetime import datetime, timezone
from typing import Any, Dict, List, Tuple

from dotenv import load_dotenv


load_dotenv()


CORE_TASKS = [
    "bug_detection_easy_1",
    "memory_leak_medium_1",
    "security_hard_1",
]


def _run_cmd(command: List[str]) -> Tuple[int, str, str]:
    """Run a command and return (returncode, stdout, stderr)."""
    result = subprocess.run(
        command,
        capture_output=True,
        text=True,
        encoding="utf-8",
        errors="replace",
    )
    return result.returncode, result.stdout, result.stderr


def _require_command(binary: str) -> bool:
    """Return True if a command exists on PATH."""
    return shutil.which(binary) is not None


def run_validation() -> Tuple[bool, Dict[str, Any]]:
    print("Running OpenEnv validation...")
    if not _require_command("openenv"):
        return False, {
            "ok": False,
            "reason": "openenv command not found on PATH",
            "stdout": "",
            "stderr": "",
        }

    code, out, err = _run_cmd(["openenv", "validate"])
    if code != 0:
        print("Validation failed")
        return False, {"ok": False, "stdout": out, "stderr": err}

    print("Validation passed")
    return True, {"ok": True, "stdout": out, "stderr": err}


def run_tests(with_coverage: bool) -> Tuple[bool, Dict[str, Any]]:
    print("Running unit tests...")
    cmd = ["pytest", "tests/", "-v"]
    coverage_enabled = False
    coverage_reason = ""
    if with_coverage and importlib.util.find_spec("pytest_cov") is not None:
        cmd.extend(["--cov=environment", "--cov-report=html"])
        coverage_enabled = True
    elif with_coverage:
        coverage_reason = "pytest-cov not installed; ran tests without coverage"

    code, out, err = _run_cmd(cmd)
    if code != 0:
        print("Tests failed")
        return False, {
            "ok": False,
            "stdout": out,
            "stderr": err,
            "coverage_enabled": coverage_enabled,
            "coverage_reason": coverage_reason,
        }

    print("Tests passed")
    return True, {
        "ok": True,
        "stdout": out,
        "stderr": err,
        "coverage_enabled": coverage_enabled,
        "coverage_reason": coverage_reason,
    }


def check_docker(image_name: str) -> Tuple[bool, Dict[str, Any]]:
    print("Checking Docker build...")
    if not _require_command("docker"):
        return False, {
            "ok": False,
            "reason": "docker command not found on PATH",
            "stdout": "",
            "stderr": "",
        }

    info_code, info_out, info_err = _run_cmd(["docker", "info"])
    if info_code != 0:
        return False, {
            "ok": False,
            "reason": "docker daemon not reachable. Start Docker Desktop and retry.",
            "stdout": info_out,
            "stderr": info_err,
        }

    code, out, err = _run_cmd(["docker", "build", "-t", image_name, "."])
    if code != 0:
        print("Docker build failed")
        return False, {"ok": False, "stdout": out, "stderr": err}

    print("Docker build successful")
    return True, {"ok": True, "stdout": out, "stderr": err}


def _inference_env_ready() -> Tuple[bool, str]:
    if not (os.getenv("API_BASE_URL") or "").strip():
        return False, "API_BASE_URL is not set"
    if not (os.getenv("MODEL_NAME") or "").strip():
        return False, "MODEL_NAME is not set"
    token = (os.getenv("API_KEY") or os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY") or "").strip()
    if not token:
        return False, "API_KEY is not set (supported aliases: HF_TOKEN, OPENAI_API_KEY)"
    return True, ""


def run_baseline(tasks: List[str], max_steps: int) -> Tuple[bool, Dict[str, Any], Dict[str, float]]:
    """Run inference for each task and collect task_score from result JSONs."""
    print("Running baseline inference for core tasks...")

    ready, reason = _inference_env_ready()
    if not ready:
        print(f"Skipping baseline inference: {reason}")
        return False, {"ok": False, "reason": reason}, {}

    baseline_scores: Dict[str, float] = {}
    details: Dict[str, Any] = {"ok": True, "runs": []}

    for task_id in tasks:
        output_file = f"baseline_{task_id}.json"
        cmd = [
            sys.executable,
            "inference.py",
            "--task-id",
            task_id,
            "--max-steps",
            str(max_steps),
            "--output",
            output_file,
        ]
        code, out, err = _run_cmd(cmd)
        run_info: Dict[str, Any] = {
            "task_id": task_id,
            "ok": code == 0,
            "stdout": out,
            "stderr": err,
            "output_file": output_file,
        }

        if code != 0:
            details["ok"] = False
            details["runs"].append(run_info)
            continue

        # Inference currently catches model-call exceptions and can still exit 0
        # after using fallback actions. Treat this as a baseline failure signal.
        combined_logs = f"{out}\n{err}".lower()
        if "error getting action from llm" in combined_logs or "insufficient balance" in combined_logs:
            details["ok"] = False
            run_info["ok"] = False
            run_info["reason"] = "Model API call failed; fallback action used"

        try:
            with open(output_file, "r", encoding="utf-8") as fh:
                payload = json.load(fh)
            score = float(payload.get("task_score", 0.0))
            baseline_scores[task_id] = score
            run_info["task_score"] = score
        except (OSError, json.JSONDecodeError, ValueError) as exc:
            details["ok"] = False
            run_info["ok"] = False
            run_info["parse_error"] = str(exc)

        details["runs"].append(run_info)

    if details["ok"]:
        print("Baseline inference passed for all selected tasks")
    else:
        print("Baseline inference had failures")

    return bool(details["ok"]), details, baseline_scores


def generate_report(
    checks: Dict[str, Dict[str, Any]],
    baseline_scores: Dict[str, float],
    report_path: str,
) -> None:
    openenv_ok = checks["validation"]["ok"]
    tests_ok = checks["tests"]["ok"]
    docker_ok = checks["docker"]["ok"]
    baseline_ok = checks["baseline"]["ok"]

    report = {
        "project": "code-review-agent-env",
        "generated_at_utc": datetime.now(timezone.utc).isoformat(),
        "tasks": CORE_TASKS,
        "difficulties": ["easy", "medium", "hard"],
        "openenv_compliant": openenv_ok,
        "docker_supported": docker_ok,
        "tests_passed": tests_ok,
        "baseline_passed": baseline_ok,
        "baseline_scores": baseline_scores,
        "checks": checks,
    }

    with open(report_path, "w", encoding="utf-8") as fh:
        json.dump(report, fh, indent=2)

    print(f"Submission report generated: {report_path}")


def main() -> int:
    parser = argparse.ArgumentParser(description="Pre-submission checklist for OpenEnv hackathon")
    parser.add_argument(
        "--skip-baseline",
        action="store_true",
        help="Skip inference baseline runs",
    )
    parser.add_argument(
        "--max-steps",
        type=int,
        default=50,
        help="Max steps for each baseline inference run",
    )
    parser.add_argument(
        "--no-coverage",
        action="store_true",
        help="Run tests without coverage output",
    )
    parser.add_argument(
        "--image-name",
        default="code-review-env",
        help="Docker image name for validation build",
    )
    parser.add_argument(
        "--report-path",
        default="submission_report.json",
        help="Where to write the JSON report",
    )
    args = parser.parse_args()

    print("=" * 50)
    print("Pre-submission Checklist")
    print("=" * 50)

    checks: Dict[str, Dict[str, Any]] = {}

    ok, detail = run_validation()
    checks["validation"] = detail

    ok, detail = run_tests(with_coverage=not args.no_coverage)
    checks["tests"] = detail

    ok, detail = check_docker(args.image_name)
    checks["docker"] = detail

    baseline_scores: Dict[str, float] = {}
    if args.skip_baseline:
        checks["baseline"] = {"ok": False, "skipped": True, "reason": "Skipped by --skip-baseline"}
    else:
        ok, detail, baseline_scores = run_baseline(CORE_TASKS, max_steps=args.max_steps)
        checks["baseline"] = detail

    generate_report(checks, baseline_scores, args.report_path)

    required_checks_ok = (
        checks["validation"]["ok"]
        and checks["tests"]["ok"]
        and checks["docker"]["ok"]
    )

    if required_checks_ok:
        print("\nRequired checks passed. Ready for submission.")
        return 0

    print("\nSome required checks failed. Please fix before submitting.")
    return 1


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