File size: 10,659 Bytes
d3cd20c
 
 
 
 
 
536dda7
 
 
 
 
 
 
 
 
 
 
 
 
d3cd20c
 
 
 
 
 
536dda7
d3cd20c
 
 
 
 
 
 
 
 
ee14542
 
 
d3cd20c
ee14542
 
d3cd20c
ee14542
 
 
 
d3cd20c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77e65fb
d3cd20c
 
 
536dda7
 
 
 
 
77e65fb
 
 
 
 
 
 
 
 
 
d3cd20c
 
 
 
 
 
 
 
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
 
 
 
536dda7
 
d3cd20c
 
 
 
 
 
 
536dda7
 
d3cd20c
 
ee14542
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
"""Catalogue of hidden 'black-box' Python functions the agent must reproduce.

Each entry pairs the reference implementation with a *typed input domain*
generator so the verifier can fuzz it, plus a public signature/docstring shown
to the agent in the prompt. The reference implementation itself is never
shown to the agent.

Each spec also carries:

* ``difficulty`` -- one of ``"easy" | "medium" | "hard"``. The trainer / eval
  harnesses use this for curriculum scheduling (easy first, then medium, ...).
  Inspired by the curriculum recommendation in Masud et al. (2026) §C2 and the
  reward-horizon shortening discussion in Ibrahim et al. (2024) §IV-F.

* ``edge_cases`` -- a list of must-be-included probe inputs the verifier
  *always* injects on top of the random fuzz batch. This makes the execution
  reward robust against agents that learn to handle the random regime but miss
  edge cases (Masud et al. (2026) C1: proxy-failure mitigation; Ibrahim et al.
  (2024) §III-A: deceptive-reward mitigation).
"""

from __future__ import annotations

import random
import string
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List


# ----- Reference implementations --------------------------------------------


def _fibonacci(n: int) -> int:
    if not isinstance(n, int) or isinstance(n, bool) or n <= 0 or n > 90:
        raise ValueError("Input must be a positive integer <= 90.")
    a, b = 0, 1
    for _ in range(n - 1):
        a, b = b, a + b
    return b if n > 0 else a


def _reverse_string(s: str) -> str:
    if not isinstance(s, str):
        raise TypeError("Input must be a string.")
    return s[::-1]


def _is_palindrome(s: str) -> bool:
    if not isinstance(s, str):
        raise TypeError("Input must be a string.")
    cleaned = "".join(ch.lower() for ch in s if ch.isalnum())
    return cleaned == cleaned[::-1]


def _digit_sum(n: int) -> int:
    if not isinstance(n, int) or isinstance(n, bool):
        raise TypeError("Input must be int.")
    if n < 0:
        raise ValueError("Input must be non-negative.")
    return sum(int(c) for c in str(n))


def _count_vowels(s: str) -> int:
    if not isinstance(s, str):
        raise TypeError("Input must be a string.")
    return sum(1 for c in s.lower() if c in "aeiou")


def _gcd(pair) -> int:
    """Greatest common divisor of two non-negative ints, given as a 2-tuple
    or 2-list. Hidden trick: tuple/list both accepted, ints only."""
    if not isinstance(pair, (list, tuple)) or len(pair) != 2:
        raise TypeError("Input must be a 2-element list or tuple.")
    a, b = pair
    if not all(isinstance(x, int) and not isinstance(x, bool) for x in (a, b)):
        raise TypeError("Both elements must be int.")
    if a < 0 or b < 0:
        raise ValueError("Both elements must be non-negative.")
    while b:
        a, b = b, a % b
    return a


def _sort_unique(xs) -> list:
    """Return sorted unique elements from a list of ints."""
    if not isinstance(xs, list):
        raise TypeError("Input must be a list.")
    if not all(isinstance(x, int) and not isinstance(x, bool) for x in xs):
        raise TypeError("All elements must be int.")
    return sorted(set(xs))


def _caesar_cipher(s: str) -> str:
    """Caesar shift by +3 on lowercase letters; everything else unchanged."""
    if not isinstance(s, str):
        raise TypeError("Input must be a string.")
    out = []
    for ch in s:
        if "a" <= ch <= "z":
            out.append(chr((ord(ch) - ord("a") + 3) % 26 + ord("a")))
        else:
            out.append(ch)
    return "".join(out)


def _is_prime(n: int) -> bool:
    if not isinstance(n, int) or isinstance(n, bool):
        raise TypeError("Input must be int.")
    if n < 2:
        return False
    if n < 4:
        return True
    if n % 2 == 0:
        return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True


# ----- Fuzz input generators ------------------------------------------------


def _fuzz_small_pos_int(rng: random.Random, n: int) -> List[int]:
    return [rng.randint(1, 30) for _ in range(n)]


def _fuzz_fib_int(rng: random.Random, n: int) -> List[int]:
    pool = [1, 2, 3, 10, 20, 30, 50, 89, 90]
    return [rng.choice(pool) if rng.random() < 0.3 else rng.randint(1, 90) for _ in range(n)]


def _fuzz_short_string(rng: random.Random, n: int) -> List[str]:
    alpha = string.ascii_letters + string.digits
    return ["".join(rng.choices(alpha, k=rng.randint(0, 12))) for _ in range(n)]


def _fuzz_palindrome_string(rng: random.Random, n: int) -> List[str]:
    out = []
    for _ in range(n):
        if rng.random() < 0.4:
            base = "".join(rng.choices(string.ascii_lowercase, k=rng.randint(0, 6)))
            out.append(base + base[::-1])
        else:
            out.append("".join(rng.choices(string.ascii_letters + " ", k=rng.randint(0, 12))))
    return out


def _fuzz_nonneg_int(rng: random.Random, n: int) -> List[int]:
    return [rng.randint(0, 10_000) for _ in range(n)]


def _fuzz_int_pair(rng: random.Random, n: int):
    return [(rng.randint(0, 1000), rng.randint(0, 1000)) for _ in range(n)]


def _fuzz_int_list(rng: random.Random, n: int):
    return [
        [rng.randint(-50, 50) for _ in range(rng.randint(0, 8))] for _ in range(n)
    ]


def _fuzz_lower_string(rng: random.Random, n: int) -> List[str]:
    return [
        "".join(rng.choices(string.ascii_lowercase + " ,!", k=rng.randint(0, 16)))
        for _ in range(n)
    ]


def _fuzz_prime_int(rng: random.Random, n: int) -> List[int]:
    seeded = [0, 1, 2, 3, 4, 9, 11, 15, 17, 25, 29, 97, 100]
    return [rng.choice(seeded) if rng.random() < 0.3 else rng.randint(0, 200) for _ in range(n)]


# ----- Spec ----------------------------------------------------------------


@dataclass(frozen=True)
class FunctionSpec:
    name: str
    fn: Callable[..., Any]
    signature: str
    description: str
    fuzzer: Callable[[random.Random, int], list]
    difficulty: str = "medium"
    # `edge_cases` are *always* probed by the verifier on top of the random
    # fuzz batch. They are scored as their own category ("edge") so the
    # verifier can report stratified pass-rates back to the trainer.
    edge_cases: List[Any] = field(default_factory=list)
    # Calling convention. When False (the default, used by all 9 builtins),
    # ``fn(arg)`` is invoked with a single positional argument -- whatever
    # the fuzzer produced. When True (used by the auto-fuzzer-generated
    # specs for multi-parameter target functions), each fuzz input is a
    # *tuple of args* and is unpacked: ``fn(*args)``.
    unpack_args: bool = False
    # Provenance: where this spec came from. Useful for /tasks?source=...
    # Defaults to "builtin" for backwards compatibility with the original
    # 9 hand-written specs.
    source: str = "builtin"


BLACK_BOX_FUNCTIONS: Dict[str, FunctionSpec] = {
    spec.name: spec
    for spec in [
        FunctionSpec(
            name="fibonacci",
            fn=_fibonacci,
            signature="fibonacci(n: int) -> int",
            description=(
                "Returns the n-th Fibonacci number. Raises ValueError for "
                "invalid n (n must be a positive int <= 90)."
            ),
            fuzzer=_fuzz_fib_int,
            difficulty="easy",
            edge_cases=[1, 2, 3, 10, 89, 90],
        ),
        FunctionSpec(
            name="reverse_string",
            fn=_reverse_string,
            signature="reverse_string(s: str) -> str",
            description="Returns the reversed string. Raises TypeError for non-str.",
            fuzzer=_fuzz_short_string,
            difficulty="easy",
            edge_cases=["", "a", "ab", "racecar", "Hello, World!"],
        ),
        FunctionSpec(
            name="is_palindrome",
            fn=_is_palindrome,
            signature="is_palindrome(s: str) -> bool",
            description=(
                "Case-insensitive palindrome check, ignoring non-alphanumeric "
                "characters. Raises TypeError for non-str."
            ),
            fuzzer=_fuzz_palindrome_string,
            difficulty="medium",
            edge_cases=["", "a", "ab", "abba", "A man, a plan, a canal: Panama", "Hello"],
        ),
        FunctionSpec(
            name="digit_sum",
            fn=_digit_sum,
            signature="digit_sum(n: int) -> int",
            description=(
                "Sum of the decimal digits of n. n must be a non-negative int."
            ),
            fuzzer=_fuzz_nonneg_int,
            difficulty="easy",
            edge_cases=[0, 1, 9, 10, 99, 100, 9999],
        ),
        FunctionSpec(
            name="count_vowels",
            fn=_count_vowels,
            signature="count_vowels(s: str) -> int",
            description="Count of vowels (a/e/i/o/u, case-insensitive) in s.",
            fuzzer=_fuzz_lower_string,
            difficulty="easy",
            edge_cases=["", "bcd", "AEIOU", "Hello, World!", "aaaaa"],
        ),
        FunctionSpec(
            name="gcd",
            fn=_gcd,
            signature="gcd(pair: tuple[int, int] | list[int]) -> int",
            description=(
                "Greatest common divisor of a 2-element tuple/list of "
                "non-negative ints."
            ),
            fuzzer=_fuzz_int_pair,
            difficulty="medium",
            edge_cases=[(0, 0), (0, 7), (12, 18), (17, 13), (100, 75)],
        ),
        FunctionSpec(
            name="sort_unique",
            fn=_sort_unique,
            signature="sort_unique(xs: list[int]) -> list[int]",
            description="Sorted, deduplicated list of ints from xs.",
            fuzzer=_fuzz_int_list,
            difficulty="easy",
            edge_cases=[[], [1], [1, 1, 1], [3, 1, 2], [-5, 5, 0, -5, 5]],
        ),
        FunctionSpec(
            name="caesar_cipher",
            fn=_caesar_cipher,
            signature="caesar_cipher(s: str) -> str",
            description=(
                "Caesar shift by +3 on lowercase letters; non-lowercase chars "
                "are unchanged."
            ),
            fuzzer=_fuzz_lower_string,
            difficulty="hard",
            edge_cases=["", "abc", "xyz", "Hello, World!", "ABC", "hello world"],
        ),
        FunctionSpec(
            name="is_prime",
            fn=_is_prime,
            signature="is_prime(n: int) -> bool",
            description="True iff n is a prime int. n must be int.",
            fuzzer=_fuzz_prime_int,
            difficulty="medium",
            edge_cases=[0, 1, 2, 3, 4, 17, 25, 97, 100],
        ),
    ]
}