File size: 4,570 Bytes
93f2b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Killable external execution boundary for ModeBench Python functions."""

from __future__ import annotations

import json
import os
from pathlib import Path
import select
import subprocess
import sys
import threading
from typing import Any, Mapping

from .python_modebench import PythonFactorValidation


class PythonFactorVerifierProcess:
    """Execute restricted candidate functions outside the trainer process."""

    def __init__(self, *, timeout_seconds: float = 1.0) -> None:
        if timeout_seconds <= 0:
            raise ValueError("timeout_seconds must be positive")
        self.timeout_seconds = float(timeout_seconds)
        self._process: subprocess.Popen[str] | None = None
        self._owner_pid = os.getpid()
        self._lock = threading.Lock()

    def _start(self) -> subprocess.Popen[str]:
        if self._owner_pid != os.getpid():
            self._process = None
            self._owner_pid = os.getpid()
        process = self._process
        if process is not None and process.poll() is None:
            return process
        worker_env = os.environ.copy()
        source_root = str(Path(__file__).resolve().parents[1])
        inherited_pythonpath = worker_env.get("PYTHONPATH")
        worker_env["PYTHONPATH"] = (
            source_root
            if not inherited_pythonpath
            else source_root + os.pathsep + inherited_pythonpath
        )
        isolated_entrypoint = (
            "import runpy,sys;"
            f"sys.path.insert(0,{source_root!r});"
            "runpy.run_module('oat_drgrpo.python_modebench_worker',run_name='__main__')"
        )
        self._process = subprocess.Popen(
            [sys.executable, "-I", "-c", isolated_entrypoint],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=None,
            text=True,
            encoding="utf-8",
            bufsize=1,
            start_new_session=True,
            env=worker_env,
        )
        return self._process

    def _stop(self) -> None:
        process, self._process = self._process, None
        if process is None:
            return
        if process.poll() is None:
            process.terminate()
            try:
                process.wait(timeout=1)
            except subprocess.TimeoutExpired:
                process.kill()
                process.wait(timeout=1)
        if process.stdin is not None:
            process.stdin.close()
        if process.stdout is not None:
            process.stdout.close()

    def close(self) -> None:
        with self._lock:
            self._stop()

    def validate(
        self,
        candidate: str,
        spec: Mapping[str, Any],
    ) -> PythonFactorValidation | None:
        request = json.dumps(
            {"candidate": str(candidate), "spec": dict(spec)},
            allow_nan=False,
        )
        with self._lock:
            for attempt in range(2):
                process = self._start()
                try:
                    assert process.stdin is not None and process.stdout is not None
                    process.stdin.write(request + "\n")
                    process.stdin.flush()
                    readable, _, _ = select.select(
                        [process.stdout], [], [], self.timeout_seconds
                    )
                    if not readable:
                        self._stop()
                        return None
                    line = process.stdout.readline()
                    if not line:
                        raise BrokenPipeError("Python ModeBench worker closed stdout")
                    payload = json.loads(line)
                    if not payload.get("valid"):
                        return None
                    return PythonFactorValidation(
                        canonical_key=str(payload["canonical_key"]),
                        outputs=tuple(int(value) for value in payload["outputs"]),
                    )
                except (BrokenPipeError, OSError, ValueError, json.JSONDecodeError):
                    self._stop()
                    if attempt:
                        return None
        return None

    def __del__(self) -> None:
        try:
            self._stop()
        except Exception:
            pass


_SHARED_VERIFIER = PythonFactorVerifierProcess()


def validate_python_factor_function_external(
    candidate: str,
    spec: Mapping[str, Any],
) -> PythonFactorValidation | None:
    """Validate a candidate through the shared external Python worker."""

    return _SHARED_VERIFIER.validate(candidate, spec)