| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import json |
| import os |
| import re |
| import subprocess |
| import sys |
| import tempfile |
| import time |
|
|
| TOTAL_BUDGET_S = 100.0 |
| PER_TASK_S = 40.0 |
| |
| |
| |
| |
| |
| STRESS_MIN_LEFT_S = 20.0 |
| MAX_CALLS = 3 |
| CASE_TIMEOUT_S = 4.0 |
| STRESS_ROUNDS = 8 |
|
|
| PRIMARY = "openai/gpt-5.6-luna" |
| REPAIR = "google/gemini-3.6-flash" |
| REFEREE = "google/gemini-3.6-flash" |
|
|
| CODE_MARKER = "Write a complete Python 3 program" |
| _SAMPLE_HDR = re.compile(r"^\s*Sample\s+(Input|Output)\s+(\d+)\s*$", re.M) |
|
|
| |
| |
| |
| FORMAT_RULE = ( |
| "\n\nGrading: your output is compared token-by-token against the expected output, exactly. " |
| "Any error tolerance stated in the problem is NOT applied. Match the formatting of this " |
| "statement's own sample outputs precisely: same digits after the decimal point, same " |
| "integer-vs-decimal choice, same separators and line breaks. " |
| "Output ONLY the program source: no prose, no Markdown fences." |
| ) |
|
|
| REFEREE_RULE = ( |
| "\n\nWrite TWO short Python 3 programs for the problem above, each in its own ``` fence:\n" |
| "1. A reference solution: the most obviously-correct approach straight from the definition, " |
| "however slow. It only ever runs on tiny inputs. Same input and output format.\n" |
| "2. A generator: `import sys, random; random.seed(int(sys.argv[1]))`, then print ONE random " |
| "VALID input. Use the smallest sizes the constraints allow so the reference finishes instantly, " |
| "and respect every stated constraint. Output only the two programs." |
| ) |
|
|
| _RETRY_MARK = "/tmp/koth-self-test-attempt" |
| _RETRY_WINDOW_S = 900.0 |
|
|
|
|
| def _is_retry(): |
| |
| |
| |
| |
| |
| try: |
| if os.path.exists(_RETRY_MARK) and time.time() - os.path.getmtime(_RETRY_MARK) < _RETRY_WINDOW_S: |
| return True |
| with open(_RETRY_MARK, "w") as f: |
| f.write(str(time.time())) |
| except Exception: |
| pass |
| return False |
|
|
|
|
| def _block_after(text, start): |
| |
| |
| |
| lines = text[start:].split("\n") |
| i = 0 |
| while i < len(lines) and not lines[i].strip(): |
| i += 1 |
| out = [] |
| while i < len(lines) and lines[i].strip(): |
| out.append(lines[i].rstrip()) |
| i += 1 |
| return "\n".join(out) |
|
|
|
|
| def parse_samples(prompt): |
| marks = list(_SAMPLE_HDR.finditer(prompt)) |
| blocks = {} |
| for m in marks: |
| blocks[(m.group(1), m.group(2))] = _block_after(prompt, m.end()) |
| pairs = [] |
| for (kind, num), body in blocks.items(): |
| if kind == "Input" and ("Output", num) in blocks: |
| exp = blocks[("Output", num)] |
| if body.strip() and exp.strip(): |
| pairs.append((int(num), body + "\n", exp)) |
| return [(i, e) for _, i, e in sorted(pairs)] |
|
|
|
|
| def extract_code(text): |
| |
| |
| t = str(text or "") |
| if "```" in t: |
| for b in (b for b in t.split("```") if b.strip()): |
| b = b[len("python"):] if b.lstrip().lower().startswith("python") else b |
| if "input" in b or "print" in b: |
| return b.strip() + "\n" |
| return t.strip() + "\n" |
|
|
|
|
| def _blocks(text): |
| parts = str(text or "").split("```") |
| out = [] |
| for b in parts[1::2]: |
| b = b[len("python"):] if b.lstrip().lower().startswith("python") else b |
| if "input" in b or "print" in b or "sys.std" in b: |
| out.append(b.strip() + "\n") |
| return out |
|
|
|
|
| def _run(code, stdin, deadline, argv=()): |
| budget = min(CASE_TIMEOUT_S, max(0.5, deadline - time.monotonic())) |
| try: |
| with tempfile.TemporaryDirectory() as d: |
| p = os.path.join(d, "sol.py") |
| with open(p, "w") as f: |
| f.write(code) |
| r = subprocess.run([sys.executable, p, *argv], input=stdin, capture_output=True, |
| text=True, timeout=budget) |
| if r.returncode != 0: |
| return "", (r.stderr or "").strip()[-400:] or f"exit {r.returncode}" |
| return r.stdout, "" |
| except subprocess.TimeoutExpired: |
| return "", f"timed out after {budget:.0f}s" |
| except Exception as e: |
| return "", f"__unavailable__:{type(e).__name__}: {e}" |
|
|
|
|
| def check(code, samples, deadline): |
| if not code.strip(): |
| return 0, "the response contained no program" |
| passed, first = 0, None |
| for stdin, expected in samples: |
| got, note = _run(code, stdin, deadline) |
| if note.startswith("__unavailable__"): |
| return len(samples), "" |
| if got.split() == expected.split(): |
| passed += 1 |
| elif first is None: |
| first = (stdin, expected, got, note) |
| if time.monotonic() > deadline: |
| break |
| if first is None: |
| return passed, "" |
| stdin, expected, got, note = first |
| detail = f"it errored: {note}" if note else f"it printed:\n{got.strip()[:600] or '(nothing)'}" |
| return passed, (f"On this input:\n{stdin.strip()[:600]}\n\nthe expected output is:\n" |
| f"{expected.strip()[:600]}\n\nbut {detail}") |
|
|
|
|
| def stress(sol, brute, gen, samples, deadline, rounds=STRESS_ROUNDS): |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if not (brute and gen): |
| return "" |
| agreed = 0 |
| for stdin, expected in samples: |
| got, note = _run(brute, stdin, deadline) |
| if note.startswith("__unavailable__"): |
| return "" |
| if note: |
| continue |
| if got.split() != expected.split(): |
| return "" |
| agreed += 1 |
| if not agreed: |
| return "" |
| for seed in range(1, rounds + 1): |
| if time.monotonic() > deadline - 2.0: |
| break |
| case, note = _run(gen, "", deadline, argv=[str(seed)]) |
| if note or not case.strip(): |
| continue |
| a, na = _run(sol, case, deadline) |
| b, nb = _run(brute, case, deadline) |
| if nb or not b.strip(): |
| continue |
| if na or a.split() != b.split(): |
| detail = f"it errored: {na}" if na else f"it printed:\n{a.strip()[:400] or '(nothing)'}" |
| return (f"On this input:\n{case.strip()[:600]}\n\na direct brute-force implementation " |
| f"of the statement prints:\n{b.strip()[:400]}\n\nbut {detail}") |
| return "" |
|
|
|
|
| def build_agent(weights): |
| try: |
| cfg = json.loads(weights.decode() if isinstance(weights, bytes) else weights or "{}") |
| except Exception: |
| cfg = {} |
| primary = cfg.get("primary", PRIMARY) |
| repair = cfg.get("repair", REPAIR) |
| referee = cfg.get("referee", REFEREE) |
| use_stress = bool(cfg.get("stress", True)) |
| base = {"max_tokens": int(cfg.get("max_tokens", 16384)), |
| "reasoning": {"effort": cfg.get("effort", "medium")}} |
| state = {"t0": None, "retry": _is_retry()} |
|
|
| def agent(prompt, call_model): |
| if state["t0"] is None: |
| state["t0"] = time.monotonic() |
| hard = state["t0"] + TOTAL_BUDGET_S |
| deadline = min(hard, time.monotonic() + PER_TASK_S) |
| p = dict(base) |
|
|
| if CODE_MARKER not in prompt: |
| p["reasoning"] = {"effort": "low"} |
| return call_model(primary, [{"role": "user", "content": prompt}], p) |
|
|
| left = hard - time.monotonic() |
| |
| |
| grade = "high" if left > 45 else "medium" if left > 22 else "low" |
| if state["retry"]: |
| grade, left = "low", min(left, 25.0) |
| p["reasoning"] = {"effort": grade} |
| else: |
| p["reasoning"] = {"effort": cfg.get("effort", grade)} |
| p["max_tokens"] = min(int(p["max_tokens"]), max(2048, int(left * 110))) |
|
|
| ask = prompt + FORMAT_RULE |
| best = call_model(primary, [{"role": "user", "content": ask}], p) |
| samples = parse_samples(prompt) |
| if not samples: |
| return best |
| code = extract_code(best) |
| best_pass, report = check(code, samples, deadline) |
|
|
| if not report and use_stress and not state["retry"] and \ |
| hard - time.monotonic() > STRESS_MIN_LEFT_S: |
| fast = {"max_tokens": 4096, "reasoning": {"effort": "low"}} |
| ref = call_model(referee, [{"role": "user", "content": prompt + REFEREE_RULE}], fast) |
| bl = _blocks(ref) |
| if len(bl) >= 2: |
| report = stress(code, bl[0], bl[1], samples, deadline) |
| if report: |
| best_pass = -1 |
| if not report: |
| return best |
|
|
| calls = 1 |
| for model in (repair, primary): |
| if calls >= MAX_CALLS or time.monotonic() > deadline - 3.0: |
| break |
| calls += 1 |
| fix = call_model(model, [{"role": "user", "content": ask}, |
| {"role": "assistant", "content": str(best)}, |
| {"role": "user", "content": |
| f"Your program is wrong.\n\n{report}\n\nFind the actual bug — " |
| f"the algorithm, an edge case, or the output format. Reply " |
| f"with the corrected program only, in one ``` fence."}], |
| dict(p)) |
| fcode = extract_code(fix) |
| n, rep = check(fcode, samples, deadline) |
| if not rep: |
| return fcode |
| if n > best_pass: |
| best, best_pass, report = fcode, n, rep |
| return best |
|
|
| return agent |
|
|