#!/usr/bin/env python3 """Self-contained check for the `sys.stdin.buffer` harness bug. Trusts nothing. Run with no arguments for levels 1-2 (a few seconds, no data needed): python scripts/verify_stdin_buffer_bug.py Add a run directory from tts-sft/round2-oss-matched to also run level 3 against real shipped results: python scripts/verify_stdin_buffer_bug.py --run-dir /exp3_120b/node03 Level 1 a property of Python itself — no repo, no data, no analysis involved. Level 2 this repo's grading harness, on a hand-written solution that is obviously correct, at the current commit and at the pre-fix commit. Level 3 real shipped candidates the fleet graded 0/16, re-executed here. Exit code 0 = the bug is absent (harness handles .buffer). 1 = present. """ from __future__ import annotations import argparse import io import json import os import subprocess import sys import tempfile REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Correct solution to "read two ints, print their sum", written the way gpt-oss # writes it. There is no argument to be had about whether this is correct. BUFFER_SOLUTION = "import sys\na, b = map(int, sys.stdin.buffer.read().split())\nprint(a + b)\n" PLAIN_SOLUTION = "a, b = map(int, input().split())\nprint(a + b)\n" WRONG_SOLUTION = "a, b = map(int, input().split())\nprint(a - b)\n" TESTS = {"inputs": ["2 3\n"], "outputs": ["5\n"], "testtype": "stdin"} def level1() -> bool: print("LEVEL 1 — plain Python, nothing to do with this repo") print(" sys.stdin = io.StringIO('2 3'); sys.stdin.buffer.read()") old = sys.stdin sys.stdin = io.StringIO("2 3\n") try: sys.stdin.buffer.read() print(" -> no error (this Python has .buffer on StringIO?!)\n") return True except AttributeError as e: print(f" -> AttributeError: {e}") print(" Any candidate reading input this way dies here, correct or not.\n") return False finally: sys.stdin = old def _verdict(harness: str, code: str) -> dict: cf = tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) cf.write(code); cf.close() tf = tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) json.dump(TESTS, tf); tf.close() try: p = subprocess.run([sys.executable, harness, cf.name, tf.name], capture_output=True, text=True, timeout=60) out = p.stdout.strip().splitlines() return json.loads(out[-1]) if out else {"error": (p.stderr or "")[:200]} finally: os.unlink(cf.name); os.unlink(tf.name) def level2() -> bool: print("LEVEL 2 — this repo's grading harness on a hand-written correct solution") harness = os.path.join(REPO, "scripts", "lcb_exec_harness.py") old_harness = None try: # the harness as it was before the fix, straight out of git blob = subprocess.run( ["git", "-C", REPO, "show", "HEAD~1:scripts/lcb_exec_harness.py"], capture_output=True, text=True, timeout=30) if blob.returncode == 0 and "def run_stdin" in blob.stdout: f = tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) f.write(blob.stdout); f.close() old_harness = f.name except Exception: # noqa: BLE001 pass ok = True rows = [("sys.stdin.buffer.read() [correct]", BUFFER_SOLUTION, True), ("input() [correct]", PLAIN_SOLUTION, True), ("input(), prints a-b [WRONG] ", WRONG_SOLUTION, False)] print(f" {'candidate':38} {'pre-fix':>12} {'current':>12} {'expected':>10}") for label, code, want in rows: cur = bool(_verdict(harness, code).get("passed")) pre = "n/a" if old_harness: pre = "PASS" if _verdict(old_harness, code).get("passed") else "fail" print(f" {label:38} {pre:>12} {'PASS' if cur else 'fail':>12}" f" {'PASS' if want else 'fail':>10}") if cur != want: ok = False if old_harness: os.unlink(old_harness) print(" A correct solution scored 'fail' is the bug. A wrong one must still fail.\n") return ok def level3(run_dir: str) -> bool: print(f"LEVEL 3 — real shipped candidates from {run_dir}") sys.path.insert(0, os.path.join(REPO, "scripts")) sys.path.insert(0, os.path.join(REPO, "handoff", "scripts")) import csv from score_diversify_ab import extract_code from grade_v2_lib import _job_for from lcb_grading import _run_once csv_path = os.path.join(run_dir, "final_grade_per_problem.csv") graded0 = [r["id"] for r in csv.DictReader(open(csv_path)) if int(r["correct_count"]) == 0 and r["format"] == "stdin_stdout"] print(f" the fleet graded {len(graded0)} stdin problems 0/16; checking the first 5") pool = {} for line in open(os.path.join(run_dir, "pool.jsonl")): r = json.loads(line) pool[r["seed_id"]] = r want = set(graded0[:5]) outs = {} for line in open(os.path.join(run_dir, "loops.out.jsonl")): d = json.loads(line) if d["id"] in want: outs[d["id"]] = d bad = 0 for pid in graded0[:5]: harness, tj, n, tl = _job_for(pool[pid], REPO, 8.0) code = extract_code((outs[pid].get("candidates") or [""])[0]) v = _run_once(harness, code, tj, n, tl) or {} ok = bool(v.get("passed")) bad += ok print(f" {pid:>12} fleet said 0/16 | here: " f"{'PASSES all ' + str(n) + ' ground-truth tests' if ok else 'fails: ' + str(v.get('error'))[:50]}") print(f" {bad}/5 of the fleet's zeros actually pass.\n") return bad == 0 if __name__ == "__main__": ap = argparse.ArgumentParser() ap.add_argument("--run-dir", help="a node dir with final_grade_per_problem.csv, " "pool.jsonl and loops.out.jsonl") a = ap.parse_args() level1() clean = level2() if a.run_dir: clean = level3(a.run_dir) and clean print("VERDICT:", "harness handles .buffer — bug absent" if clean else "correct solutions are being scored wrong — bug present") sys.exit(0 if clean else 1)