File size: 5,746 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
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
"""Killable external MuJoCo boundary for maze language-action programs."""

from __future__ import annotations

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

from .maze_modebench import MazeValidation


class MazeVerifierProcess:
    """Execute maze action programs outside the trainer process."""

    def __init__(
        self,
        *,
        timeout_seconds: float = 25.0,
        worker_python: str | Path | None = None,
    ) -> None:
        if timeout_seconds <= 0:
            raise ValueError("timeout_seconds must be positive")
        root = Path(
            os.environ.get(
                "OAT_ZERO_REPO_ROOT", Path(__file__).resolve().parents[2]
            )
        ).resolve()
        configured = worker_python or os.environ.get("OAT_ZERO_MAZE_WORKER_PYTHON")
        self.worker_python = Path(configured) if configured else (
            root / "var/maze_runtime/venv/bin/python"
        )
        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
        if not self.worker_python.is_file():
            raise FileNotFoundError(f"maze worker Python is missing: {self.worker_python}")
        source_root = str(Path(__file__).resolve().parents[1])
        entrypoint = (
            "import runpy,sys;"
            f"sys.path.insert(0,{source_root!r});"
            "runpy.run_module('oat_drgrpo.maze_modebench_worker',run_name='__main__')"
        )
        worker_env = os.environ.copy()
        worker_env.setdefault("MUJOCO_GL", "egl")
        self._process = subprocess.Popen(
            [str(self.worker_python), "-B", "-I", "-c", 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],
    ) -> MazeValidation | None:
        result = self.validate_with_execution(candidate, spec)
        return result[0] if result is not None else None

    def validate_with_execution(
        self,
        candidate: str,
        spec: Mapping[str, Any],
    ) -> tuple[MazeValidation, dict[str, Any]] | None:
        """Return validation plus the trusted raw record for offline audits."""

        request = json.dumps(
            {"candidate": str(candidate), "spec": dict(spec)},
            allow_nan=False,
        )
        with self._lock:
            for attempt in range(2):
                try:
                    process = self._start()
                    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("maze worker closed stdout")
                    payload = json.loads(line)
                    if not payload.get("valid"):
                        return None
                    validation = MazeValidation(
                        canonical_key=str(payload["canonical_key"]),
                        directed_gates=tuple(
                            str(value) for value in payload["directed_gates"]
                        ),
                        action_tokens=tuple(
                            str(value) for value in payload["action_tokens"]
                        ),
                        simulator_steps=int(payload["simulator_steps"]),
                    )
                    return validation, dict(payload["execution"])
                except (
                    BrokenPipeError,
                    FileNotFoundError,
                    OSError,
                    ValueError,
                    json.JSONDecodeError,
                ):
                    self._stop()
                    if attempt:
                        return None
        return None

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


_SHARED_VERIFIER = MazeVerifierProcess()


def validate_maze_action_program_external(
    candidate: str,
    spec: Mapping[str, Any],
) -> MazeValidation | None:
    """Validate a candidate through the shared pinned MuJoCo worker."""

    return _SHARED_VERIFIER.validate(candidate, spec)