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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Rule-based code reward for Stage 3 verl/DAPO smoke tests."""

from __future__ import annotations

import ast
import json
import os
import re
import resource
import signal
import subprocess
import sys
import tempfile
from typing import Any


FENCE_RE = re.compile(r"```(?:python|py)?\s*(.*?)```", re.DOTALL | re.IGNORECASE)
MAX_PARSE_CODE_CHARS = 65_536
MAX_PARSE_TRIM_ATTEMPTS = 64

STATUS_CODE = {
    "passed": 0,
    "syntax_error": 1,
    "missing_entry_point": 2,
    "entry_point_not_callable": 3,
    "wrong_answer": 4,
    "runtime_error": 5,
    "timeout": 6,
    "harness_error": 7,
    "no_tests": 8,
    "eval_error": 9,
}

EVAL_HARNESS = r'''
import ast
import io
import json
import math
import signal
import sys
import traceback
from bisect import *
from collections import *
from functools import *
from heapq import *
from itertools import *
from math import *
from operator import *
from string import *
from typing import *

payload = json.loads(sys.stdin.read())
code = payload["code"]
mode = payload["mode"]
entry_point = payload.get("entry_point")
tests = payload.get("tests") or []
per_test_timeout = float(payload.get("per_test_timeout", 2.0))

class FakeStdin:
    def __init__(self, text):
        self._text = io.StringIO(str(text))
        self.buffer = io.BytesIO(str(text).encode())
    def read(self, *args):
        return self._text.read(*args)
    def readline(self, *args):
        return self._text.readline(*args)
    def readlines(self, *args):
        return self._text.readlines(*args)
    def __iter__(self):
        return iter(self._text)

def norm_text(value):
    text = "" if value is None else str(value)
    return "\n".join(line.rstrip() for line in text.strip().splitlines()).strip()

def stdout_equal(got, expected):
    a, b = norm_text(got), norm_text(expected)
    return a == b or a.split() == b.split()

def value_equal(a, b):
    if isinstance(a, bool) or isinstance(b, bool):
        return a == b
    if isinstance(a, (int, float)) and isinstance(b, (int, float)):
        return math.isclose(float(a), float(b), rel_tol=1e-6, abs_tol=1e-6)
    if isinstance(a, (list, tuple)) and isinstance(b, (list, tuple)):
        return len(a) == len(b) and all(value_equal(x, y) for x, y in zip(a, b))
    if isinstance(a, dict) and isinstance(b, dict):
        return set(a.keys()) == set(b.keys()) and all(value_equal(a[k], b[k]) for k in a)
    return str(a) == str(b)

def result(**kwargs):
    print(json.dumps(kwargs, ensure_ascii=False))

def timeout_handler(signum, frame):
    raise TimeoutError("per-test timeout")

try:
    ast.parse(code)
except (SyntaxError, ValueError, MemoryError, RecursionError) as exc:
    result(
        passed=False,
        status="syntax_error",
        interface_ok=False,
        passed_tests=0,
        total_tests=len(tests),
        wrong_answers=0,
        runtime_errors=0,
        timeouts=0,
        error=str(exc)[:300],
    )
    raise SystemExit

try:
    if not tests:
        result(
            passed=False,
            status="no_tests",
            interface_ok=False,
            passed_tests=0,
            total_tests=0,
            wrong_answers=0,
            runtime_errors=0,
            timeouts=0,
        )
        raise SystemExit

    if mode == "function":
        ns = {"__name__": "__main__"}
        signal.signal(signal.SIGALRM, timeout_handler)
        try:
            signal.setitimer(signal.ITIMER_REAL, per_test_timeout)
            exec(compile(code, "<solution>", "exec"), ns)
        except TimeoutError as exc:
            result(
                passed=False,
                status="timeout",
                interface_ok=False,
                passed_tests=0,
                total_tests=len(tests),
                wrong_answers=0,
                runtime_errors=0,
                timeouts=len(tests),
                error=str(exc)[:300],
            )
            raise SystemExit
        finally:
            signal.setitimer(signal.ITIMER_REAL, 0)
        try:
            candidate = eval(entry_point, ns)
        except Exception as exc:
            result(
                passed=False,
                status="missing_entry_point",
                interface_ok=False,
                passed_tests=0,
                total_tests=len(tests),
                wrong_answers=0,
                runtime_errors=0,
                timeouts=0,
                error=str(exc)[:300],
            )
            raise SystemExit
        if not callable(candidate):
            result(
                passed=False,
                status="entry_point_not_callable",
                interface_ok=False,
                passed_tests=0,
                total_tests=len(tests),
                wrong_answers=0,
                runtime_errors=0,
                timeouts=0,
            )
            raise SystemExit

        passed_tests = 0
        wrong_answers = 0
        runtime_errors = 0
        timeouts = 0
        for idx, test in enumerate(tests):
            args = test.get("args") or []
            kwargs = test.get("kwargs") or {}
            expected = test.get("output")
            try:
                signal.setitimer(signal.ITIMER_REAL, per_test_timeout)
                got = candidate(*args, **kwargs)
                if value_equal(got, expected):
                    passed_tests += 1
                else:
                    wrong_answers += 1
            except TimeoutError:
                timeouts += 1
            except Exception:
                runtime_errors += 1
            finally:
                signal.setitimer(signal.ITIMER_REAL, 0)
            if timeouts >= 3:
                break
        skipped_after_timeout = len(tests) - (passed_tests + wrong_answers + runtime_errors + timeouts)
        timeouts += max(skipped_after_timeout, 0)
        passed = passed_tests == len(tests)
        status = "passed" if passed else "timeout" if timeouts else "runtime_error" if runtime_errors else "wrong_answer"
        result(
            passed=passed,
            status=status,
            interface_ok=True,
            passed_tests=passed_tests,
            total_tests=len(tests),
            wrong_answers=wrong_answers,
            runtime_errors=runtime_errors,
            timeouts=timeouts,
        )
    else:
        signal.signal(signal.SIGALRM, timeout_handler)
        passed_tests = 0
        wrong_answers = 0
        runtime_errors = 0
        timeouts = 0
        for idx, test in enumerate(tests):
            old_stdin, old_stdout = sys.stdin, sys.stdout
            fake_out = io.StringIO()
            ns = {"__name__": "__main__"}
            case_failed = False
            try:
                sys.stdin = FakeStdin(test.get("input", ""))
                sys.stdout = fake_out
                try:
                    signal.setitimer(signal.ITIMER_REAL, per_test_timeout)
                    exec(compile(code, "<solution>", "exec"), ns)
                except SystemExit:
                    pass
                except TimeoutError:
                    timeouts += 1
                    case_failed = True
                except Exception:
                    runtime_errors += 1
                    case_failed = True
                finally:
                    signal.setitimer(signal.ITIMER_REAL, 0)
            finally:
                sys.stdin, sys.stdout = old_stdin, old_stdout
            if not case_failed:
                got = fake_out.getvalue()
                expected = test.get("output", "")
                if stdout_equal(got, expected):
                    passed_tests += 1
                else:
                    wrong_answers += 1
            if timeouts >= 3:
                break
        skipped_after_timeout = len(tests) - (passed_tests + wrong_answers + runtime_errors + timeouts)
        timeouts += max(skipped_after_timeout, 0)
        passed = passed_tests == len(tests)
        status = "passed" if passed else "timeout" if timeouts else "runtime_error" if runtime_errors else "wrong_answer"
        result(
            passed=passed,
            status=status,
            interface_ok=True,
            passed_tests=passed_tests,
            total_tests=len(tests),
            wrong_answers=wrong_answers,
            runtime_errors=runtime_errors,
            timeouts=timeouts,
        )
except TimeoutError as exc:
    result(
        passed=False,
        status="timeout",
        interface_ok=True,
        passed_tests=0,
        total_tests=len(tests),
        wrong_answers=0,
        runtime_errors=0,
        timeouts=len(tests),
        error=str(exc)[:300],
    )
except Exception as exc:
    result(
        passed=False,
        status="runtime_error",
        interface_ok=True,
        passed_tests=0,
        total_tests=len(tests),
        wrong_answers=0,
        runtime_errors=len(tests),
        timeouts=0,
        error=str(exc)[:300],
        traceback=traceback.format_exc()[-500:],
    )
'''


def _json_loads_maybe(value: Any) -> Any:
    if isinstance(value, str):
        try:
            return json.loads(value)
        except Exception:
            return {}
    return value if isinstance(value, dict) else {}


def _trim_parseable_code(code: str) -> str:
    # Generated text can contain NUL bytes, which make ast.parse raise
    # ValueError instead of SyntaxError. They are never valid Python source.
    text = code.replace("\x00", "").strip()[:MAX_PARSE_CODE_CHARS]
    if not text:
        return text
    lines = text.splitlines()
    first_end = max(1, len(lines) - MAX_PARSE_TRIM_ATTEMPTS + 1)
    for end in range(len(lines), first_end - 1, -1):
        candidate = "\n".join(lines[:end]).strip()
        if not candidate:
            continue
        try:
            ast.parse(candidate)
            return candidate
        except (SyntaxError, ValueError, MemoryError, RecursionError):
            continue
    return text


def extract_code(text: str, starter_code: str = "") -> str:
    text = text or ""
    fenced = FENCE_RE.findall(text)
    if fenced:
        code = fenced[0].strip()
    else:
        lines = text.splitlines()
        start = None
        for idx, line in enumerate(lines):
            stripped = line.strip()
            if re.match(r"(from\s+\S+\s+import\s+|import\s+\S+|class\s+\w+|def\s+\w+|if\s+__name__)", stripped):
                start = idx
                break
        code = "\n".join(lines[start:]).strip() if start is not None else text.strip()

    starter = (starter_code or "").strip()
    if starter and "class Solution" in code and starter not in code:
        code = starter.rstrip() + "\n\n" + code
    return _trim_parseable_code(code)


def _format_ok(solution_str: str) -> bool:
    text = (solution_str or "").strip()
    if not text:
        return False
    if "```" in text:
        return False
    prose_markers = ["Here is", "Explanation", "```python", "The solution"]
    return not any(marker.lower() in text[:160].lower() for marker in prose_markers)


def _set_eval_limits(memory_limit_gib: float, exec_timeout: float, output_limit_mib: float) -> None:
    memory_bytes = max(1, int(memory_limit_gib * 1024**3))
    output_bytes = max(1, int(output_limit_mib * 1024**2))
    cpu_seconds = max(1, int(exec_timeout) + 1)
    resource.setrlimit(resource.RLIMIT_AS, (memory_bytes, memory_bytes))
    resource.setrlimit(resource.RLIMIT_CPU, (cpu_seconds, cpu_seconds))
    resource.setrlimit(resource.RLIMIT_FSIZE, (output_bytes, output_bytes))
    resource.setrlimit(resource.RLIMIT_NOFILE, (128, 128))
    resource.setrlimit(resource.RLIMIT_NPROC, (256, 256))


def _kill_process_group(proc: subprocess.Popen[str]) -> None:
    try:
        os.killpg(proc.pid, signal.SIGKILL)
    except ProcessLookupError:
        pass


def _run_eval(
    code: str,
    meta: dict[str, Any],
    exec_timeout: float,
    per_test_timeout: float,
    memory_limit_gib: float,
    output_limit_mib: float,
) -> dict[str, Any]:
    payload = {
        "code": code,
        "mode": meta.get("mode"),
        "entry_point": meta.get("entry_point"),
        "tests": meta.get("tests") or [],
        "per_test_timeout": per_test_timeout,
    }
    proc = None
    try:
        with (
            tempfile.TemporaryFile(mode="w+t", encoding="utf-8") as stdout_file,
            tempfile.TemporaryFile(mode="w+t", encoding="utf-8") as stderr_file,
        ):
            proc = subprocess.Popen(
                [sys.executable, "-c", EVAL_HARNESS],
                stdin=subprocess.PIPE,
                stdout=stdout_file,
                stderr=stderr_file,
                text=True,
                start_new_session=True,
                preexec_fn=lambda: _set_eval_limits(memory_limit_gib, exec_timeout, output_limit_mib),
            )
            try:
                proc.communicate(input=json.dumps(payload, ensure_ascii=False), timeout=exec_timeout)
            except subprocess.TimeoutExpired:
                _kill_process_group(proc)
                proc.communicate()
                raise
            _kill_process_group(proc)
            stdout_file.seek(0)
            stdout = stdout_file.read()

        parsed = None
        for line in reversed(stdout.strip().splitlines()):
            candidate = _json_loads_maybe(line)
            if isinstance(candidate, dict) and "passed" in candidate and "total_tests" in candidate:
                parsed = candidate
                break
        if parsed is None:
            return {
                "passed": False,
                "status": "harness_error",
                "interface_ok": False,
                "passed_tests": 0,
                "total_tests": len(payload["tests"]),
                "wrong_answers": 0,
                "runtime_errors": len(payload["tests"]),
                "timeouts": 0,
            }
        return parsed
    except subprocess.TimeoutExpired:
        return {
            "passed": False,
            "status": "timeout",
            "interface_ok": True,
            "passed_tests": 0,
            "total_tests": len(payload["tests"]),
            "wrong_answers": 0,
            "runtime_errors": 0,
            "timeouts": len(payload["tests"]),
        }
    except Exception:
        return {
            "passed": False,
            "status": "eval_error",
            "interface_ok": False,
            "passed_tests": 0,
            "total_tests": len(payload["tests"]),
            "wrong_answers": 0,
            "runtime_errors": len(payload["tests"]),
            "timeouts": 0,
        }


def compute_score(
    data_source: str,
    solution_str: str,
    ground_truth: Any,
    extra_info: dict[str, Any] | None = None,
    exec_timeout: float = 6.0,
    per_test_timeout: float = 2.0,
    memory_limit_gib: float = 2.0,
    output_limit_mib: float = 16.0,
) -> dict[str, float]:
    """Return full-pass bonus or capped partial-credit execution reward."""
    del data_source
    meta = _json_loads_maybe(ground_truth)
    if not meta and extra_info:
        meta = _json_loads_maybe(extra_info)

    tests = meta.get("tests") or []
    format_ok = _format_ok(solution_str)
    try:
        code = extract_code(solution_str, meta.get("starter_code") or "")
        ast.parse(code)
        parse_ok = True
    except (SyntaxError, ValueError, MemoryError, RecursionError):
        code = ""
        parse_ok = False

    if not tests:
        eval_result = {
            "passed": False,
            "status": "no_tests",
            "interface_ok": False,
            "passed_tests": 0,
            "total_tests": 0,
        }
    elif not parse_ok:
        eval_result = {
            "passed": False,
            "status": "syntax_error",
            "interface_ok": False,
            "passed_tests": 0,
            "total_tests": len(tests),
        }
    else:
        try:
            eval_result = _run_eval(
                code,
                meta,
                exec_timeout=exec_timeout,
                per_test_timeout=per_test_timeout,
                memory_limit_gib=memory_limit_gib,
                output_limit_mib=output_limit_mib,
            )
        except (Exception, MemoryError) as exc:
            eval_result = {
                "passed": False,
                "status": "eval_error",
                "interface_ok": False,
                "passed_tests": 0,
                "total_tests": len(tests),
                "error": str(exc)[:300],
            }

    total_tests = int(eval_result.get("total_tests") or len(tests) or 0)
    passed_tests = int(eval_result.get("passed_tests") or 0)
    pass_frac = passed_tests / max(total_tests, 1)
    interface_ok = bool(eval_result.get("interface_ok"))
    passed = bool(eval_result.get("passed"))
    status = str(eval_result.get("status") or "eval_error")

    # A fully correct solution gets 1.0. Any imperfect solution is capped at
    # 0.8 and scaled by its test pass fraction. For example, 70/80 -> 0.7.
    score = 1.0 if total_tests > 0 and passed else 0.8 * pass_frac

    return {
        "score": score,
        "acc": 1.0 if passed else 0.0,
        "format_ok": float(format_ok),
        "parse_ok": float(parse_ok),
        "interface_ok": float(interface_ok),
        "pass_frac": float(pass_frac),
        "passed": 1.0 if passed else 0.0,
        "passed_tests": float(passed_tests),
        "total_tests": float(total_tests),
        "wrong_answers": float(eval_result.get("wrong_answers") or 0),
        "runtime_errors": float(eval_result.get("runtime_errors") or 0),
        "timeouts": float(eval_result.get("timeouts") or 0),
        "status_code": float(STATUS_CODE.get(status, STATUS_CODE["eval_error"])),
    }