File size: 8,552 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
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
"""Restricted executable Python tasks for ModeBench.

The model writes one pure ``lambda n: ...`` expression.  A separate worker
process evaluates the function on a frozen prompt-local test suite.  A response
is correct when every returned integer is a proper divisor of its input.  The
semantic outcome is the complete vector returned by that same execution.

This module owns the syntax and task contract.  The actual call to model code
is made only by :mod:`oat_drgrpo.python_modebench_worker`.
"""

from __future__ import annotations

import ast
from dataclasses import dataclass
from math import prod
from typing import Any, Mapping


PYTHON_FACTOR_VERIFIER = "python_factor_function"
PYTHON_FACTOR_VERSION = "factor-v1"
_MAX_CANDIDATE_CHARS = 240
_MAX_AST_NODES = 64
_MAX_CASES = 8

_ALLOWED_NODE_TYPES = (
    ast.Expression,
    ast.Lambda,
    ast.arguments,
    ast.arg,
    ast.IfExp,
    ast.BoolOp,
    ast.And,
    ast.Or,
    ast.Compare,
    ast.Eq,
    ast.NotEq,
    ast.Lt,
    ast.LtE,
    ast.Gt,
    ast.GtE,
    ast.BinOp,
    ast.Add,
    ast.Sub,
    ast.Mult,
    ast.FloorDiv,
    ast.Mod,
    ast.UnaryOp,
    ast.UAdd,
    ast.USub,
    ast.Not,
    ast.Name,
    ast.Load,
    ast.Constant,
)


class PythonModeBenchError(ValueError):
    """Raised for a malformed task or candidate program."""


@dataclass(frozen=True)
class PythonFactorValidation:
    """Result of one successful external execution."""

    canonical_key: str
    outputs: tuple[int, ...]


def _canonical_python_route_ast(node: ast.AST) -> str:
    """Return a literal-abstracted strategy skeleton for restricted Python."""

    if isinstance(node, ast.Expression):
        return _canonical_python_route_ast(node.body)
    if isinstance(node, ast.Lambda):
        return f"lambda({_canonical_python_route_ast(node.body)})"
    if isinstance(node, ast.Name):
        return "input"
    if isinstance(node, ast.Constant):
        return "literal"
    if isinstance(node, ast.UnaryOp):
        return (
            f"{type(node.op).__name__.lower()}"
            f"({_canonical_python_route_ast(node.operand)})"
        )
    if isinstance(node, ast.BinOp):
        op = type(node.op).__name__.lower()
        children = [
            _canonical_python_route_ast(node.left),
            _canonical_python_route_ast(node.right),
        ]
        if isinstance(node.op, (ast.Add, ast.Mult)):
            children.sort()
        return f"{op}({','.join(children)})"
    if isinstance(node, ast.Compare):
        return "compare(" + ",".join(
            [
                _canonical_python_route_ast(node.left),
                *(type(op).__name__.lower() for op in node.ops),
                *(
                    _canonical_python_route_ast(comparator)
                    for comparator in node.comparators
                ),
            ]
        ) + ")"
    if isinstance(node, ast.BoolOp):
        values = [_canonical_python_route_ast(value) for value in node.values]
        values.sort()
        return f"{type(node.op).__name__.lower()}({','.join(values)})"
    if isinstance(node, ast.IfExp):
        return (
            f"if({_canonical_python_route_ast(node.test)},"
            f"{_canonical_python_route_ast(node.body)},"
            f"{_canonical_python_route_ast(node.orelse)})"
        )
    raise PythonModeBenchError(
        f"cannot canonicalize Python route node {type(node).__name__}"
    )


def python_factor_route_signature(candidate: str) -> str:
    """Return code-structure identity after the restricted parser accepts it."""

    parsed = parse_python_factor_candidate(candidate)
    return "python-factor-route:v1:" + _canonical_python_route_ast(parsed)


def proper_divisors(value: int) -> tuple[int, ...]:
    """Return all positive proper divisors other than one."""

    value = int(value)
    if value < 4:
        return ()
    return tuple(divisor for divisor in range(2, value) if value % divisor == 0)


def python_factor_mode_count(cases: tuple[int, ...] | list[int]) -> int:
    """Return the exact number of valid behavior vectors for a task."""

    divisor_sets = [proper_divisors(value) for value in cases]
    if any(not divisors for divisors in divisor_sets):
        return 0
    return prod(len(divisors) for divisors in divisor_sets)


def parse_python_factor_spec(spec: Mapping[str, Any]) -> tuple[int, ...]:
    """Validate a trusted dataset specification and return its tool inputs."""

    if spec.get("verifier") != PYTHON_FACTOR_VERIFIER:
        raise PythonModeBenchError("wrong Python ModeBench verifier")
    if spec.get("python_version") != PYTHON_FACTOR_VERSION:
        raise PythonModeBenchError("unsupported Python ModeBench version")
    raw_cases = spec.get("cases")
    if not isinstance(raw_cases, list) or not 2 <= len(raw_cases) <= _MAX_CASES:
        raise PythonModeBenchError("cases must contain between two and eight inputs")
    if any(isinstance(value, bool) or not isinstance(value, int) for value in raw_cases):
        raise PythonModeBenchError("case inputs must be integers")
    cases = tuple(int(value) for value in raw_cases)
    if len(set(cases)) != len(cases):
        raise PythonModeBenchError("case inputs must be unique")
    if any(value < 4 or value > 1_000 for value in cases):
        raise PythonModeBenchError("case inputs are outside the bounded domain")
    if python_factor_mode_count(cases) < 2:
        raise PythonModeBenchError("task does not have multiple valid modes")
    return cases


def parse_python_factor_candidate(candidate: str) -> ast.Expression:
    """Parse the bounded, call-free lambda language."""

    text = str(candidate).strip()
    if not text or len(text) > _MAX_CANDIDATE_CHARS:
        raise PythonModeBenchError("candidate is empty or too long")
    try:
        parsed = ast.parse(text, mode="eval")
    except (SyntaxError, ValueError) as error:
        raise PythonModeBenchError("candidate is not one Python expression") from error
    if not isinstance(parsed.body, ast.Lambda):
        raise PythonModeBenchError("candidate must be a lambda")
    arguments = parsed.body.args
    if (
        len(arguments.args) != 1
        or arguments.args[0].arg != "n"
        or arguments.posonlyargs
        or arguments.kwonlyargs
        or arguments.vararg is not None
        or arguments.kwarg is not None
        or arguments.defaults
        or arguments.kw_defaults
    ):
        raise PythonModeBenchError("lambda must have the exact signature lambda n")
    nodes = list(ast.walk(parsed))
    if len(nodes) > _MAX_AST_NODES:
        raise PythonModeBenchError("candidate AST is too large")
    for node in nodes:
        if not isinstance(node, _ALLOWED_NODE_TYPES):
            raise PythonModeBenchError(
                f"unsupported Python syntax: {type(node).__name__}"
            )
        if isinstance(node, ast.Name) and node.id != "n":
            raise PythonModeBenchError(f"unknown name: {node.id}")
        if isinstance(node, ast.Constant):
            if isinstance(node.value, bool) or not isinstance(node.value, int):
                raise PythonModeBenchError("only integer literals are allowed")
            if abs(int(node.value)) > 10_000:
                raise PythonModeBenchError("integer literal is too large")
    return parsed


def execute_python_factor_candidate(
    candidate: str,
    spec: Mapping[str, Any],
) -> PythonFactorValidation:
    """Execute one already restricted candidate.

    This entry point is worker-only.  Callers in the trainer and evaluator use
    ``validate_python_factor_function_external`` so model code never executes
    in their process.
    """

    cases = parse_python_factor_spec(spec)
    parsed = parse_python_factor_candidate(candidate)
    code = compile(parsed, "<modebench-python-factor>", "eval")
    function = eval(code, {"__builtins__": {}}, {})  # noqa: S307
    outputs: list[int] = []
    for value in cases:
        output = function(value)
        if isinstance(output, bool) or not isinstance(output, int):
            raise PythonModeBenchError("function output must be an integer")
        output = int(output)
        if output <= 1 or output >= value or value % output != 0:
            raise PythonModeBenchError(
                f"function returned {output}, not a proper divisor of {value}"
            )
        outputs.append(output)
    output_tuple = tuple(outputs)
    key = "python_factor:" + ",".join(str(value) for value in output_tuple)
    return PythonFactorValidation(canonical_key=key, outputs=output_tuple)