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
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
# SPDX-License-Identifier: BSD-3-Clause

"""Software engineering: repair seeded bugs in a small Python module.

The agent edits `lib.py` with exact-match replacements and runs the visible
tests. Grading runs hidden tests in a fresh sandbox: the reward is the share of
buggy functions whose hidden tests all pass, and 0 if a function that was
correct got broken. At difficulty 3 one bug, and at difficulty 4 two bugs, have no
failing visible test, so the agent has to check the code against SPEC.md.
"""

import datetime as dt
import json
import re

from ..sandbox import Workspace
from .base import Episode, Family, Tool, ToolError, arg, file_tools, submit_action

# Test-case argument generators used by FUNCTIONS below.


def _luhn_case(r):
    """A digit string (sometimes grouped by dashes) whose check digit is right 60% of the time."""
    body = [r.randint(0, 9) for _ in range(r.randint(1, 15))]
    total = 0
    for i, d in enumerate(reversed(body)):
        if i % 2 == 0:
            d *= 2
            d -= 9 if d > 9 else 0
        total += d
    check = (10 - total % 10) % 10
    digits = body + [check if r.random() < 0.6 else (check + r.randint(1, 9)) % 10]
    text = "".join(map(str, digits))
    return "-".join(text[i : i + 4] for i in range(0, len(text), 4)) if r.random() < 0.4 else text


def _duration_case(r):
    if r.random() < 0.3:  # units in any order, upper case
        units = r.sample(["h", "m", "s"], r.randint(1, 3))
        return ("".join(f"{r.randint(1, 59)}{u}" for u in units).upper(),)
    units = sorted(r.sample(["h", "m", "s"], r.randint(1, 3)), key="hms".index)
    return ("".join(f"{r.randint(1, 59)}{u}" for u in units),)


def _roman(n):
    out = ""
    for value, sym in [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")]:
        while n >= value:
            out, n = out + sym, n - value
    return out


def _business_case(r):
    start = dt.date(2026, 1, 1) + dt.timedelta(r.randint(0, 300))
    end = start + dt.timedelta(r.randint(0, 25))
    holidays = [(start + dt.timedelta(r.randint(0, 25))).isoformat() for _ in range(r.randint(0, 3))]
    return (start.isoformat(), end.isoformat(), holidays)


# name -> (source, spec line, bugs [(old, new)], case generator)
FUNCTIONS = {
    "clamp_mean": (
        '''def clamp_mean(xs, lo, hi):
    if not xs:
        return 0.0
    total = 0.0
    for x in xs:
        total += min(max(x, lo), hi)
    return total / len(xs)
''',
        "clamp_mean(xs, lo, hi): mean of the values after clipping each to [lo, hi]; 0.0 for an empty list.",
        [("min(max(x, lo), hi)", "max(min(x, lo), hi)"), ("return total / len(xs)", "return total / (len(xs) - 1)"), ("        return 0.0\n", "        return None\n")],
        lambda r: ([r.randint(-20, 20) for _ in range(r.randint(0, 6))], r.randint(-10, 0), r.randint(1, 10)),
    ),
    "parse_duration": (
        '''def parse_duration(text):
    units = {"h": 3600, "m": 60, "s": 1}
    total, number = 0, ""
    for ch in text.strip().lower():
        if ch.isdigit():
            number += ch
        elif ch in units and number:
            total += int(number) * units[ch]
            number = ""
        else:
            raise ValueError(f"bad duration: {text!r}")
    if number:
        raise ValueError(f"bad duration: {text!r}")
    return total
''',
        'parse_duration(text): seconds in a duration like "1h30m15s", "45m" or "2H"; units h, m, s in any case; ValueError otherwise.',
        [('"m": 60', '"m": 600'), ("text.strip().lower()", "text.strip()"), ("            number = \"\"\n        else:", "        else:")],
        _duration_case,
    ),
    "merge_intervals": (
        '''def merge_intervals(pairs):
    merged = []
    for start, end in sorted(pairs):
        if merged and start <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], end)
        else:
            merged.append([start, end])
    return [tuple(p) for p in merged]
''',
        "merge_intervals(pairs): merge overlapping or touching (start, end) intervals; sorted list of tuples.",
        [("start <= merged[-1][1]", "start < merged[-1][1]"), ("max(merged[-1][1], end)", "end"), ("sorted(pairs)", "pairs")],
        lambda r: ([(s, s + r.randint(0, 6)) for s in (r.randint(0, 30) for _ in range(r.randint(2, 7)))],),
    ),
    "luhn_valid": (
        '''def luhn_valid(number):
    digits = [int(c) for c in number if c.isdigit()]
    if len(digits) < 2:
        return False
    total = 0
    for i, d in enumerate(reversed(digits)):
        if i % 2 == 1:
            d *= 2
            if d > 9:
                d -= 9
        total += d
    return total % 10 == 0
''',
        "luhn_valid(number): Luhn checksum of the digits in the string (spaces and dashes ignored); fewer than 2 digits is invalid.",
        [("i % 2 == 1", "i % 2 == 0"), ("d -= 9", "d -= 10"), ("len(digits) < 2", "len(digits) < 1")],
        lambda r: (_luhn_case(r),),
    ),
    "roman_to_int": (
        '''def roman_to_int(text):
    values = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    total = 0
    for i, ch in enumerate(text):
        value = values[ch]
        if i + 1 < len(text) and values[text[i + 1]] > value:
            total -= value
        else:
            total += value
    return total
''',
        "roman_to_int(text): value of an upper-case Roman numeral such as MCMXCIV.",
        [("values[text[i + 1]] > value", "values[text[i + 1]] >= value"), ('"D": 500', '"D": 50'), ("            total -= value", "            total += value")],
        lambda r: (_roman(r.randint(1, 3999)),),
    ),
    "chunk": (
        '''def chunk(items, size):
    if size <= 0:
        raise ValueError("size must be positive")
    return [items[i : i + size] for i in range(0, len(items), size)]
''',
        "chunk(items, size): consecutive slices of length size (the last may be shorter); ValueError if size <= 0.",
        [("range(0, len(items), size)", "range(0, len(items) - size, size)"), ("items[i : i + size]", "items[i : i + size - 1]")],
        lambda r: (list(range(r.randint(0, 12))), r.randint(1, 5)),
    ),
    "top_words": (
        '''def top_words(text, k):
    counts = {}
    for raw in text.split():
        word = raw.strip(".,;:!?\\"'()").lower()
        if word:
            counts[word] = counts.get(word, 0) + 1
    ranked = sorted(counts.items(), key=lambda item: (-item[1], item[0]))
    return [word for word, _ in ranked[:k]]
''',
        'top_words(text, k): the k most frequent words, case-insensitive, ignoring surrounding punctuation .,;:!?"\'() ; ties in alphabetical order.',
        [("key=lambda item: (-item[1], item[0])", "key=lambda item: (item[1], item[0])"), (".lower()\n", "\n"), ("ranked[:k]", "ranked[: k - 1]")],
        lambda r: (" ".join(r.choice(["Alpha", "beta,", "gamma.", "alpha", "\"Beta\"", "delta!", "(gamma)", "beta", "Delta"]) for _ in range(r.randint(4, 14))), r.randint(1, 4)),
    ),
    "business_days": (
        '''import datetime as dt


def business_days(start, end, holidays=()):
    day = dt.date.fromisoformat(start)
    stop = dt.date.fromisoformat(end)
    off = {dt.date.fromisoformat(h) for h in holidays}
    count = 0
    while day < stop:
        if day.weekday() < 5 and day not in off:
            count += 1
        day += dt.timedelta(days=1)
    return count
''',
        "business_days(start, end, holidays=()): weekdays (Mon-Fri) in [start, end) that are not holidays; ISO date strings.",
        [("day.weekday() < 5", "day.weekday() < 6"), ("while day < stop", "while day <= stop"), ("day not in off", "True")],
        _business_case,
    ),
}


def _reference(name):
    scope = {}
    exec(FUNCTIONS[name][0], scope)
    return scope[name]


def _cases(name, rng, n):
    fn, make = _reference(name), FUNCTIONS[name][3]
    out, seen = [], set()
    for _ in range(n * 6):
        args = make(rng)
        key = repr(args)
        if key in seen:
            continue
        seen.add(key)
        try:
            out.append((args, fn(*args), None))
        except ValueError:
            out.append((args, None, "ValueError"))
        if len(out) == n:
            break
    return out


def _detects(name, bug, cases):
    """True if the mutated function fails at least one case."""
    scope = {}
    try:
        exec(FUNCTIONS[name][0].replace(*bug), scope)
    except SyntaxError:
        return False
    fn = scope[name]
    for args, expected, err in cases:
        try:
            got = fn(*args)
        except Exception as e:  # noqa: BLE001
            if type(e).__name__ != err:
                return True
            continue
        if err or not _same(got, expected):
            return True
    return False


def _same(a, b):
    if isinstance(a, float) or isinstance(b, float):
        return a is not None and b is not None and abs(a - b) < 1e-9
    return a == b


def _test_file(name, cases, prefix):
    lines = []
    for i, (args, expected, err) in enumerate(cases, 1):
        call = f"lib.{name}({', '.join(map(repr, args))})"
        lines.append(f"def test_{prefix}{name}_{i}():")
        if err:
            lines += [
                "    try:",
                f"        got = {call}",
                "    except ValueError:",
                "        return",
                f"    raise AssertionError(f\"{call.replace(chr(34), chr(39))} returned {{got!r}}, expected ValueError\")",
            ]
        else:
            lines += [
                f"    got = {call}",
                f"    assert _same(got, {expected!r}), f\"{call.replace(chr(34), chr(39))} returned {{got!r}}, expected {expected!r}\"",
            ]
        lines.append("")
    return "\n".join(lines)


TEST_HEADER = '''import lib


def _same(a, b):
    if isinstance(a, float) or isinstance(b, float):
        return a is not None and b is not None and abs(a - b) < 1e-9
    return a == b


'''

RUNNER = r'''
import json, sys
sys.path.insert(0, ".")
sys.dont_write_bytecode = True
results = {}
try:
    import importlib
    mod = importlib.import_module(MODULE)
except Exception as e:
    print(json.dumps({"import_error": f"{type(e).__name__}: {e}"}))
    raise SystemExit
for name in sorted(n for n in dir(mod) if n.startswith("test_")):
    try:
        getattr(mod, name)()
        results[name] = "pass"
    except AssertionError as e:
        results[name] = "FAIL: " + str(e)[:200]
    except Exception as e:
        results[name] = f"ERROR: {type(e).__name__}: {str(e)[:150]}"
print(json.dumps({"results": results}))
'''


def _run_suite(ws, module):
    out = ws.run_python(f"MODULE = {module!r}\n{RUNNER}", limit=1_000_000)
    last = out.strip().splitlines()[-1] if out.strip() else ""
    try:
        return json.loads(last)
    except json.JSONDecodeError:
        return {"import_error": out[-300:]}


def make(difficulty, rng):
    # functions in lib.py, seeded bugs, and bugs that no visible test catches
    n_funcs, n_bugs, n_silent = {1: (2, 1, 0), 2: (3, 1, 0), 3: (5, 2, 1), 4: (6, 3, 2)}[difficulty]
    names = rng.sample(sorted(FUNCTIONS), n_funcs)
    buggy = rng.sample(names, n_bugs)
    bugs = {}
    hidden = {name: _cases(name, rng, 8) for name in names}
    for name in buggy:
        options = [b for b in FUNCTIONS[name][2] if _detects(name, b, hidden[name])]
        bugs[name] = rng.choice(options)

    imports = sorted({line for n in names for line in FUNCTIONS[n][0].splitlines() if line.startswith("import ")})

    def module(sources):
        """Join function sources into one file, hoisting their imports to the top."""
        sources = [re.sub(r"^import .*\n\n\n", "", b) for b in sources]
        return ("\n".join(imports) + "\n\n\n" if imports else "") + "\n\n".join(sources)

    lib = module([FUNCTIONS[n][0].replace(*bugs[n]) if n in bugs else FUNCTIONS[n][0] for n in names])
    fixed_lib = module([FUNCTIONS[n][0] for n in names])

    silent = set(rng.sample(buggy, n_silent))
    visible = []
    for name in names:
        if name in silent:  # two cases the bug gets right
            cases = [c for c in hidden[name] if not _detects(name, bugs[name], [c])][:2]
        else:
            cases = hidden[name][:3]
            if name in bugs and not _detects(name, bugs[name], cases):  # make sure one visible test fails
                cases = cases[:2] + [next(c for c in hidden[name] if _detects(name, bugs[name], [c]))]
        visible.append(_test_file(name, cases, ""))
    files = {
        "lib.py": lib,
        "test_lib.py": TEST_HEADER + "\n".join(visible),
        "SPEC.md": "# lib.py specification\n" + "\n".join(f"- {FUNCTIONS[n][1]}" for n in names) + "\n",
    }
    ws = Workspace(files)
    hidden_suite = TEST_HEADER + "\n".join(_test_file(n, hidden[n], "hidden_") for n in names)

    def check_path(args, verb):
        if arg(args, "path").strip() != "lib.py":
            raise ToolError(f"only lib.py can be {verb}")

    def save_lib(text):
        files["lib.py"] = text
        ws.write("lib.py", text)

    def do_edit(args):
        check_path(args, "edited")
        old, new = arg(args, "old"), arg(args, "new")
        count = files["lib.py"].count(old)
        if count != 1:
            raise ToolError(f'"old" must match exactly once in lib.py; it matches {count} times')
        save_lib(files["lib.py"].replace(old, new))
        if not new:
            return "edited lib.py"
        line = files["lib.py"][: files["lib.py"].index(new)].count("\n") + 1
        return f"edited lib.py at line {line}"

    def do_write(args):
        check_path(args, "written")
        save_lib(arg(args, "content"))
        return f"wrote lib.py ({len(files['lib.py'].splitlines())} lines)"

    def do_tests(args):
        report = _run_suite(ws, "test_lib")
        if "import_error" in report:
            return f"lib.py failed to import: {report['import_error']}"
        res = report["results"]
        failed = {k: v for k, v in res.items() if v != "pass"}
        lines = [f"{len(res) - len(failed)}/{len(res)} visible tests passed"]
        lines += [f"{k}: {v}" for k, v in list(failed.items())[:8]]
        return "\n".join(lines)

    def grade(answer):
        g = Workspace({"lib.py": files["lib.py"], "test_hidden.py": hidden_suite})
        try:
            report = _run_suite(g, "test_hidden")
        finally:
            g.close()
        if "import_error" in report:
            return 0.0, "lib.py does not import"
        res = report["results"]
        ok = {n: all(v == "pass" for k, v in res.items() if k.startswith(f"test_hidden_{n}_")) for n in names}
        if not all(ok[n] for n in names if n not in bugs):
            return 0.0, "a function that was correct is now broken"
        fixed = sum(ok[n] for n in bugs)
        return fixed / len(bugs), f"{fixed}/{len(bugs)} buggy functions fixed"

    tools = file_tools(ws, files, python=False) + [
        Tool("edit", '{"tool": "edit", "args": {"path": "lib.py", "old": "<exact text>", "new": "<replacement>"}}  replace text that occurs exactly once', do_edit),
        Tool("write", '{"tool": "write", "args": {"path": "lib.py", "content": "<whole file>"}}  replace lib.py', do_write),
        Tool("run_tests", '{"tool": "run_tests", "args": {}}  run test_lib.py against lib.py', do_tests),
    ]
    return Episode(
        question=f"lib.py has {n_bugs} bug{'s' if n_bugs > 1 else ''}: some functions do not match SPEC.md. Fix them without breaking the others. Hidden tests decide the grade.",
        tools=tools,
        answer_hint='When done: {"tool": "submit", "args": {"answer": {}}}',
        grade=grade,
        workspace=ws,
        oracle=[{"tool": "write", "args": {"path": "lib.py", "content": fixed_lib}}, submit_action({})],
    )


FAMILY = Family("code_repair", "software_engineering", "Fix seeded bugs in a small Python module against a spec and tests", make)