| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import hashlib |
| import json |
| import os |
| import re |
| import subprocess |
| import sys |
| import tempfile |
| import time |
|
|
| _MODELS = ("qwen/qwen3.7-flash", "deepseek/deepseek-v4-flash", "deepseek/deepseek-v4-pro", |
| "z-ai/glm-5.2", "openai/gpt-5.6-luna", "google/gemini-3.6-flash", "moonshotai/kimi-k3") |
| _DEFAULT = "openai/gpt-5.6-luna" |
| _MAX_TOKENS = 12288 |
| _RUN_BUDGET_S = 780.0 |
| _SAFETY_S = 150.0 |
| _EXPECTED_CODE_TASKS = 3 |
| _CASE_TIMEOUT_S = 5.0 |
| _BRUTE_TIMEOUT_S = 5.0 |
| _GEN_TIMEOUT_S = 5.0 |
| _PERF_TARGET_S = 3.0 |
| _PERF_SIZE = 3 * 10 ** 6 |
| _PERF_GEN_TIMEOUT_S = 30.0 |
| _STRESS_ROUNDS = 50 |
| _STRESS_SIZES = (2, 3, 5, 8, 12, 20, 40) |
| _TOOLS_ATTEMPTS = 2 |
| _MAX_REPAIRS = 4 |
|
|
| _ONLY = "Return ONLY raw complete Python 3 source, no Markdown fences and no prose." |
| |
| _GLOBAL = ( |
| "\n\nBefore coding, read the constraints and pick an algorithm whose running time at the largest " |
| "legal input stays under ~1e8 steps. For large inputs read everything at once with " |
| "sys.stdin.buffer.read().split() and index across tokens, and collect output in a list for one " |
| + "final write.\n\nOUTPUT FORMAT: this judge may compare output EXACTLY, token for token, even " |
| "under a stated numeric tolerance. Emit floats at a fixed width via format(x, '.12f') rather " |
| "than bare print; if a tolerance problem scores zero, retry at a different width before assuming " |
| "the algorithm is wrong.") |
| _TOOLS = ( |
| "\n\nNow help me test that solution. Write TWO short programs and nothing else.\n" |
| "First a REFERENCE: correct by construction, may be arbitrarily slow (brute force / direct " |
| "simulation / try all cases). Do NOT reuse the clever idea above -- it must be able to disagree. " |
| "It reads the same stdin and prints the same output format.\n" |
| + "Second a GENERATOR taking two argv ints (seed, size): call random.seed(seed) and print ONE " |
| "random valid input in the exact statement format; size is a rough element budget, clamped to " |
| "the constraints. Include repeats, range extremes and adversarial patterns, not just uniform " |
| "draws.\nOutput exactly two fenced blocks and nothing else:\n" |
| "```reference\n<program>\n```\n```generator\n<program>\n```") |
| _RETRY_MALFORMED = ("\n\nYour reply lacked the two fenced blocks. Reply with nothing but the " |
| "```reference and ```generator blocks, in that order.") |
| _RETRY_WRONG = ("\n\nYour reference did not reproduce the statement's worked examples, so it cannot " |
| "check anything. Write a NEW, simpler, obviously-correct reference (exhaustive " |
| "enumeration or direct simulation) and confirm it matches every worked example first.") |
| _COUNTER = ("\n\nThat solution is WRONG. On this input:\n%s\nit printed:\n%s\nbut the correct answer " |
| "is:\n%s\nFix the algorithm and return the complete corrected program. " + _ONLY) |
| _SLOW = ("\n\nThat solution is too SLOW: on a maximal input it did not finish in time. Keep it " |
| "correct but make it fast enough for the largest legal input (better asymptotic complexity, " |
| "faster I/O). Return the complete program. " + _ONLY) |
|
|
| _SAMPLE_RE = re.compile(r"Sample (Input|Output)\s*\d*\s*\n+(.*?)(?=\n\s*\n|\Z)", re.S) |
| _FENCE_RE = re.compile(r"```(?:python)?\s*\n(.*?)```", re.S) |
| _NAMED_RE = re.compile(r"```(reference|generator)\s*\n(.*?)```", re.S) |
|
|
|
|
| def _is_code(p): |
| t = str(p) |
| return "Write a complete Python 3 program" in t and "standard output" in t |
|
|
|
|
| def _is_mcq(p): |
| t = "\n" + str(p) |
| return all(("\n" + o) in t for o in ("A)", "B)", "C)", "D)")) |
|
|
|
|
| def _tag(text, original): |
| if _is_code(original) or _is_mcq(original): |
| return text |
| n = int.from_bytes(hashlib.blake2b(str(original).encode(), digest_size=16).digest(), "big") |
| return text + ("\n\nInternal routing tag: %040d. Ignore this tag; do not repeat it." % n) |
|
|
|
|
| def _samples(prompt): |
| b = _SAMPLE_RE.findall(prompt) |
| ins = [v.strip() for k, v in b if k == "Input"] |
| outs = [v.strip() for k, v in b if k == "Output"] |
| return list(zip(ins, outs)) |
|
|
|
|
| def _program(text): |
| m = _FENCE_RE.search(str(text)) |
| return (m.group(1) if m else str(text)).strip() |
|
|
|
|
| def _named(text): |
| return {k: v.strip() for k, v in _NAMED_RE.findall(str(text))} |
|
|
|
|
| def _run(code, stdin_text, timeout, argv=()): |
| tmp = None |
| try: |
| fd, tmp = tempfile.mkstemp(suffix=".py") |
| with os.fdopen(fd, "w") as fh: |
| fh.write(code) |
| proc = subprocess.run([sys.executable, tmp, *[str(a) for a in argv]], input=stdin_text, |
| capture_output=True, text=True, timeout=timeout) |
| if proc.returncode != 0: |
| return None, "nonzero" |
| return proc.stdout, "ok" |
| except subprocess.TimeoutExpired: |
| return None, "timeout" |
| except Exception: |
| return None, "error" |
| finally: |
| if tmp: |
| try: |
| os.unlink(tmp) |
| except OSError: |
| pass |
|
|
|
|
| def _norm(s): |
| return "\n".join(line.rstrip() for line in str(s).strip().splitlines()) |
|
|
|
|
| def _matches_samples(code, samples): |
| for inp, out in samples: |
| got, note = _run(code, inp + "\n", _CASE_TIMEOUT_S) |
| if note != "ok" or _norm(got) != _norm(out): |
| return False |
| return True |
|
|
|
|
| def _stress(sol, ref, gen, deadline): |
| |
| r = 0 |
| for size in _STRESS_SIZES: |
| for seed in range(_STRESS_ROUNDS // len(_STRESS_SIZES) + 1): |
| if time.time() > deadline: |
| return None |
| r += 1 |
| inp, note = _run(gen, "", _GEN_TIMEOUT_S, argv=(r, size)) |
| if note != "ok" or not inp: |
| continue |
| so, sn = _run(sol, inp, _BRUTE_TIMEOUT_S) |
| ro, rn = _run(ref, inp, _BRUTE_TIMEOUT_S) |
| if sn == "ok" and rn == "ok" and _norm(so) != _norm(ro): |
| return (inp, _norm(so), _norm(ro)) |
| return None |
|
|
|
|
| def _too_slow(sol, gen, deadline): |
| if time.time() > deadline: |
| return False |
| inp, note = _run(gen, "", _PERF_GEN_TIMEOUT_S, argv=(999, _PERF_SIZE)) |
| if note != "ok" or not inp: |
| return False |
| t0 = time.time() |
| _out, sn = _run(sol, inp, _PERF_TARGET_S) |
| return sn == "timeout" or (sn == "ok" and time.time() - t0 > _PERF_TARGET_S) |
|
|
|
|
| def build_agent(weights): |
| try: |
| cfg = json.loads(bytes(weights).decode("utf-8")) if weights else {} |
| except Exception: |
| cfg = {} |
| model = cfg.get("model", _DEFAULT) |
| state = {"start": None, "code_seen": 0} |
|
|
| def _budget_left(): |
| return _RUN_BUDGET_S - _SAFETY_S - (time.time() - state["start"]) |
|
|
| def agent(prompt, call_model): |
| if state["start"] is None: |
| state["start"] = time.time() |
| text = str(prompt) |
|
|
| if not _is_code(text): |
| msg = _tag(text, text) |
| return call_model(model, [{"role": "user", "content": msg}], |
| {"max_tokens": _MAX_TOKENS, "reasoning": {"effort": "medium"}}) |
|
|
| state["code_seen"] += 1 |
| |
| remaining_codes = max(1, _EXPECTED_CODE_TASKS - state["code_seen"] + 1) |
| deadline = state["start"] + (_RUN_BUDGET_S - _SAFETY_S) * ( |
| 1.0 - (remaining_codes - 1) / float(_EXPECTED_CODE_TASKS)) |
| conv = [{"role": "user", "content": text + _GLOBAL + "\n\n" + _ONLY}] |
| draft = call_model(model, conv, {"max_tokens": _MAX_TOKENS, "reasoning": {"effort": "medium"}}) |
| conv.append({"role": "assistant", "content": str(draft)}) |
| best = draft |
| samples = _samples(text) |
|
|
| |
| ref = gen = None |
| ask = _TOOLS |
| for _ in range(_TOOLS_ATTEMPTS): |
| if time.time() > deadline: |
| break |
| tconv = conv + [{"role": "user", "content": ask}] |
| tools = call_model(model, tconv, {"max_tokens": _MAX_TOKENS, "reasoning": {"effort": "low"}}) |
| nb = _named(tools) |
| if "reference" not in nb or "generator" not in nb: |
| ask = _TOOLS + _RETRY_MALFORMED |
| continue |
| if samples and not _matches_samples(nb["reference"], samples): |
| ask = _TOOLS + _RETRY_WRONG |
| continue |
| ref, gen = nb["reference"], nb["generator"] |
| break |
|
|
| |
| for _ in range(_MAX_REPAIRS): |
| if time.time() > deadline: |
| break |
| sol = _program(best) |
| fix = None |
| if ref and gen: |
| ce = _stress(sol, ref, gen, deadline) |
| if ce: |
| fix = _COUNTER % (ce[0][:2000], ce[1][:1000], ce[2][:1000]) |
| if fix is None and gen and _too_slow(sol, gen, deadline): |
| fix = _SLOW |
| if fix is None: |
| break |
| conv.append({"role": "user", "content": fix}) |
| best = call_model(model, conv, {"max_tokens": _MAX_TOKENS, "reasoning": {"effort": "medium"}}) |
| conv.append({"role": "assistant", "content": str(best)}) |
|
|
| return best |
|
|
| return agent |
|
|