75 lines
4.5 KiB
Python
75 lines
4.5 KiB
Python
"""Scripted fake model for the two-graph hardening example (offline tests).
|
|
|
|
- synthesis requests get five fixed tasks (one with a broken reference solution);
|
|
- "harder variant" requests get a fixed harder variant per task;
|
|
- agent requests get a write_file tool call, then finish. The agent always solves
|
|
`add` and `biggest`, alternates on `classify` and `add_many`, and never solves
|
|
`reverse_words` or `second_biggest`.
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
|
|
T = "from solution import *\n"
|
|
TASKS = [
|
|
{"name": "add", "statement": "Return the sum of a and b.", "signature": "def add(a, b):",
|
|
"solution": "def add(a, b):\n return a + b\n",
|
|
"tests": T + "assert add(2, 3) == 5\nassert add(-1, 1) == 0\nprint('ok')\n"},
|
|
{"name": "classify", "statement": "Return 'fizz' for multiples of 3, 'buzz' for multiples of 5, "
|
|
"'fizzbuzz' for both, else str(n).", "signature": "def classify(n):",
|
|
"solution": "def classify(n):\n s = ('fizz' if n % 3 == 0 else '') + ('buzz' if n % 5 == 0 else '')\n"
|
|
" return s or str(n)\n",
|
|
"tests": T + "assert classify(3) == 'fizz'\nassert classify(10) == 'buzz'\nassert classify(15) == 'fizzbuzz'\n"
|
|
"assert classify(7) == '7'\nprint('ok')\n"},
|
|
{"name": "reverse_words", "statement": "Reverse the order of words in s, collapsing whitespace.",
|
|
"signature": "def reverse_words(s):", "solution": "def reverse_words(s):\n return ' '.join(reversed(s.split()))\n",
|
|
"tests": T + "assert reverse_words('a b c') == 'c b a'\nassert reverse_words('') == ''\nprint('ok')\n"},
|
|
{"name": "biggest", "statement": "Return the largest element of xs.", "signature": "def biggest(xs):",
|
|
"solution": "def biggest(xs):\n return max(xs)\n", "tests": T + "assert biggest([1, 5, 2]) == 5\nprint('ok')\n"},
|
|
{"name": "broken_min", "statement": "Return the smallest element of xs.", "signature": "def smallest(xs):",
|
|
"solution": "def smallest(xs):\n return max(xs)\n", "tests": T + "assert smallest([1, 5, 2]) == 1\nprint('ok')\n"},
|
|
]
|
|
|
|
VARIANTS = {
|
|
"add": {"name": "add_many", "statement": "Return the sum of all arguments (any number, possibly none).",
|
|
"signature": "def add_many(*xs):", "solution": "def add_many(*xs):\n return sum(xs)\n",
|
|
"tests": T + "assert add_many() == 0\nassert add_many(1, 2, 3) == 6\nprint('ok')\n"},
|
|
"biggest": {"name": "second_biggest", "statement": "Return the second largest DISTINCT element of xs, or "
|
|
"None if there is none.", "signature": "def second_biggest(xs):",
|
|
"solution": "def second_biggest(xs):\n s = sorted(set(xs))\n return s[-2] if len(s) > 1 else None\n",
|
|
"tests": T + "assert second_biggest([1, 5, 5, 2]) == 2\nassert second_biggest([3, 3]) is None\n"
|
|
"print('ok')\n"},
|
|
}
|
|
|
|
ATTEMPTS = {
|
|
"add": ["def add(a, b):\n return a + b\n"],
|
|
"biggest": ["def biggest(xs):\n return max(xs)\n"],
|
|
"classify": [TASKS[1]["solution"], "def classify(n):\n return str(n)\n"],
|
|
"reverse_words": ["def reverse_words(s):\n return s[::-1]\n"],
|
|
"add_many": [VARIANTS["add"]["solution"], "def add_many(*xs):\n return xs[0] + xs[1]\n"],
|
|
"second_biggest": ["def second_biggest(xs):\n return sorted(xs)[-2]\n"],
|
|
}
|
|
_counter: dict[str, int] = {}
|
|
|
|
|
|
def respond(payload: dict):
|
|
messages = payload["messages"]
|
|
prompt = next(m["content"] for m in messages if m["role"] == "user")
|
|
if "HARDER variant" in prompt:
|
|
name = re.search(r"Task (\w+):", prompt).group(1)
|
|
return {"role": "assistant", "content": json.dumps(VARIANTS.get(name, {**TASKS[0], "name": name + "_v2"}))}
|
|
if (payload.get("response_format") or {}).get("type") == "json_schema":
|
|
return {"role": "assistant", "content": json.dumps({"items": TASKS})}
|
|
if any(m["role"] == "tool" for m in messages):
|
|
return {"role": "assistant", "content": "", "tool_calls": [
|
|
{"id": "call_2", "type": "function", "function": {"name": "finish", "arguments": '{"summary": "done"}'}}]}
|
|
name = next((n for n in ATTEMPTS if f"def {n}(" in prompt), None)
|
|
if name is None:
|
|
return {"role": "assistant", "content": "I cannot solve this."}
|
|
i = _counter.get(name, 0)
|
|
_counter[name] = i + 1
|
|
code = ATTEMPTS[name][i % len(ATTEMPTS[name])]
|
|
return {"role": "assistant", "content": "", "tool_calls": [
|
|
{"id": "call_1", "type": "function",
|
|
"function": {"name": "write_file", "arguments": json.dumps({"path": "solution.py", "content": code})}}]}
|