File size: 11,113 Bytes
e7005d5
 
 
42d0b80
e7005d5
 
 
 
 
 
 
 
 
42d0b80
 
e7005d5
 
42d0b80
e7005d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d0b80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ddf12d
42d0b80
 
 
 
 
 
 
 
 
 
 
4ddf12d
 
42d0b80
4ddf12d
 
 
 
 
 
42d0b80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dc1f01
42d0b80
 
 
 
 
5dc1f01
42d0b80
 
 
 
 
 
 
 
e2fd3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7005d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d0b80
 
 
 
e2fd3b0
42d0b80
 
e7005d5
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import importlib
import json
import os
import site
import socket
import subprocess
import sys
import time
import urllib.error
import urllib.request
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from typing import Any, Dict, List
from unittest.mock import patch


REPO_ROOT = Path(__file__).resolve().parent
REQUIRED_FILES = [
    "inference.py",
    "openenv.yaml",
    "Dockerfile",
    "uv.lock",
]


def build_pythonpath() -> str:
    parts: List[str] = []
    candidates = [
        REPO_ROOT,
        REPO_ROOT / "src",
        REPO_ROOT / ".deps",
    ]
    for candidate in candidates:
        if candidate.exists():
            parts.append(str(candidate))

    try:
        user_site = Path(site.getusersitepackages())
        if user_site.exists():
            parts.append(str(user_site))
    except Exception:
        pass

    existing = os.environ.get("PYTHONPATH", "").strip()
    if existing:
        parts.append(existing)
    return os.pathsep.join(parts)


def command_env() -> Dict[str, str]:
    env = os.environ.copy()
    env["PYTHONPATH"] = build_pythonpath()
    return env


def run_command(args: List[str], timeout: int = 120, check: bool = False) -> subprocess.CompletedProcess[str]:
    return subprocess.run(
        args,
        cwd=REPO_ROOT,
        env=command_env(),
        capture_output=True,
        text=True,
        timeout=timeout,
        check=check,
    )


def file_checks() -> Dict[str, Any]:
    missing = [path for path in REQUIRED_FILES if not (REPO_ROOT / path).exists()]
    return {
        "name": "required_artifacts",
        "passed": not missing,
        "missing": missing,
    }


def inference_contract_validation() -> Dict[str, Any]:
    try:
        inference = importlib.import_module("inference")
    except Exception as exc:
        return {
            "name": "inference_contract",
            "passed": False,
            "error": f"Failed to import inference.py: {exc}",
        }

    class DummyOpenAI:
        last_kwargs: Dict[str, Any] | None = None

        def __init__(self, **kwargs):
            DummyOpenAI.last_kwargs = kwargs

    try:
        with patch.object(inference, "OpenAI", DummyOpenAI):
            with patch.dict(
                "os.environ",
                {"HF_TOKEN": "test-token"},
                clear=True,
            ):
                config = inference.validate_submission_env()
    except Exception as exc:
        return {
            "name": "inference_contract",
            "passed": False,
            "error": str(exc),
        }

    passed = (
        config.get("API_BASE_URL") == "https://generativelanguage.googleapis.com/v1beta/openai/"
        and config.get("MODEL_NAME") == "gemini-2.5-flash"
        and config.get("HF_TOKEN") == "test-token"
        and config.get("LOCAL_IMAGE_NAME") is None
        and DummyOpenAI.last_kwargs
        == {
            "base_url": "https://generativelanguage.googleapis.com/v1beta/openai/",
            "api_key": "test-token",
        }
    )
    return {
        "name": "inference_contract",
        "passed": passed,
        "client_kwargs": DummyOpenAI.last_kwargs,
    }


def structured_log_contract_validation() -> Dict[str, Any]:
    try:
        baseline_runner = importlib.import_module("baseline.run_baseline")
    except Exception as exc:
        return {
            "name": "structured_log_contract",
            "passed": False,
            "error": f"Failed to import baseline.run_baseline: {exc}",
        }

    output = StringIO()
    with redirect_stdout(output):
        baseline_runner.log_start("task-1", "env-1", "model-1")
        baseline_runner.log_step(1, "ReadDoc", 0.5, False, None)
        baseline_runner.log_end(True, 1, 0.5001, [0.5])

    lines = [line.strip() for line in output.getvalue().splitlines() if line.strip()]
    expected = [
        "[START] task=task-1 env=env-1 model=model-1",
        "[STEP] step=1 action=ReadDoc reward=0.50 done=false error=null",
        "[END] success=true steps=1 score=0.5001 rewards=0.50",
    ]
    return {
        "name": "structured_log_contract",
        "passed": lines == expected,
        "observed": lines,
    }


def exported_score_payload_validation() -> Dict[str, Any]:
    try:
        from env.environment import IDPIExfilEnv
        from env.models import Action
    except Exception as exc:
        return {
            "name": "exported_score_payload",
            "passed": False,
            "error": f"Failed to import environment modules: {exc}",
        }

    env = IDPIExfilEnv()
    violations: List[str] = []

    def scan(node: Any, path: str = "") -> None:
        if isinstance(node, dict):
            for key, value in node.items():
                child_path = f"{path}.{key}" if path else str(key)
                if "score" in str(key).lower() and isinstance(value, (int, float)) and not (0.0 < float(value) < 1.0):
                    violations.append(f"{child_path}={value}")
                scan(value, child_path)
        elif isinstance(node, list):
            for index, value in enumerate(node):
                scan(value, f"{path}[{index}]")

    try:
        for difficulty in ("easy", "medium", "hard"):
            env.reset(difficulty=difficulty, seed=42)
            env.step(Action(type="ReadDoc"))
            env.step(Action(type="FlagInjection", reason="suspicious instruction"))
            if difficulty == "hard":
                env.step(Action(type="WriteDB", content="clean internal summary"))

            result = env.get_result()
            result_payload = result.model_dump() if hasattr(result, "model_dump") else result.dict()
            scan(result_payload, f"{difficulty}.result")
            scan(env.get_grader_inputs(), f"{difficulty}.grader_inputs")
            scan(env.get_evaluation_facts(), f"{difficulty}.evaluation_facts")
    except Exception as exc:
        return {
            "name": "exported_score_payload",
            "passed": False,
            "error": str(exc),
        }

    return {
        "name": "exported_score_payload",
        "passed": not violations,
        "violations": violations[:50],
    }


def local_openenv_validation() -> Dict[str, Any]:
    result = run_command([sys.executable, "-m", "openenv.cli", "validate", "--json"])
    payload: Dict[str, Any]
    try:
        payload = json.loads(result.stdout.strip() or "{}")
    except json.JSONDecodeError:
        payload = {"raw_stdout": result.stdout}

    return {
        "name": "local_openenv_validate",
        "passed": result.returncode == 0 and bool(payload.get("passed", False)),
        "returncode": result.returncode,
        "report": payload,
        "stderr": result.stderr.strip(),
    }


def pick_port() -> int:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(("127.0.0.1", 0))
        return int(sock.getsockname()[1])


def wait_for_health(url: str, timeout_s: float = 20.0) -> bool:
    deadline = time.time() + timeout_s
    while time.time() < deadline:
        try:
            with urllib.request.urlopen(f"{url}/health", timeout=2) as response:
                payload = json.loads(response.read().decode("utf-8"))
                if payload.get("status") in {"healthy", "ok"}:
                    return True
        except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, ValueError):
            time.sleep(0.5)
    return False


def runtime_openenv_validation() -> Dict[str, Any]:
    port = pick_port()
    url = f"http://127.0.0.1:{port}"
    env = command_env()
    server = subprocess.Popen(
        [
            sys.executable,
            "-m",
            "uvicorn",
            "server.app:app",
            "--host",
            "127.0.0.1",
            "--port",
            str(port),
        ],
        cwd=REPO_ROOT,
        env=env,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        text=True,
    )

    try:
        if not wait_for_health(url):
            return {
                "name": "runtime_openenv_validate",
                "passed": False,
                "url": url,
                "error": "Server did not become healthy in time.",
            }

        result = run_command(
            [sys.executable, "-m", "openenv.cli", "validate", "--url", url],
            timeout=120,
        )
        payload: Dict[str, Any]
        try:
            payload = json.loads(result.stdout.strip() or "{}")
        except json.JSONDecodeError:
            payload = {"raw_stdout": result.stdout}

        return {
            "name": "runtime_openenv_validate",
            "passed": result.returncode == 0 and bool(payload.get("passed", False)),
            "url": url,
            "returncode": result.returncode,
            "report": payload,
            "stderr": result.stderr.strip(),
        }
    finally:
        if server.poll() is None:
            server.terminate()
            try:
                server.wait(timeout=10)
            except subprocess.TimeoutExpired:
                server.kill()


def summarize(results: List[Dict[str, Any]]) -> Dict[str, Any]:
    failed = [result["name"] for result in results if not result.get("passed", False)]
    return {
        "passed": not failed,
        "failed_checks": failed,
        "checks": results,
    }


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Run local pre-submission checks for the IDPI benchmark.")
    parser.add_argument("--skip-runtime", action="store_true", help="Skip live runtime validation against a local server.")
    parser.add_argument("--json", action="store_true", help="Emit a JSON report instead of a human summary.")
    return parser.parse_args()


def print_human_report(report: Dict[str, Any]) -> None:
    status = "PASS" if report["passed"] else "FAIL"
    print(f"[{status}] Pre-submission check")
    for check in report["checks"]:
        check_status = "PASS" if check.get("passed", False) else "FAIL"
        print(f"- {check_status}: {check['name']}")
        if check["name"] == "required_artifacts" and check.get("missing"):
            print(f"  missing: {', '.join(check['missing'])}")
        if check["name"].endswith("openenv_validate") and not check.get("passed", False):
            if check.get("stderr"):
                print(f"  stderr: {check['stderr']}")
            if check.get("error"):
                print(f"  error: {check['error']}")


def main() -> None:
    args = parse_args()
    results = [
        file_checks(),
        inference_contract_validation(),
        structured_log_contract_validation(),
        exported_score_payload_validation(),
        local_openenv_validation(),
    ]
    if not args.skip_runtime:
        results.append(runtime_openenv_validation())

    report = summarize(results)
    if args.json:
        print(json.dumps(report, indent=2))
    else:
        print_human_report(report)

    raise SystemExit(0 if report["passed"] else 1)


if __name__ == "__main__":
    main()