File size: 5,287 Bytes
7f7e3bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | """How exactly does the untrained model fail the library bug fixes? Rebuild each calibration episode's planted bug from
its seed and check what the model's edits touched.
Usage: .venv/bin/python scripts/wrong_fixes.py > reports/wrong-answers/bugfix.txt
"""
import collections
import glob
import json
import random
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "scripts"))
from calibrate import first_json_object # noqa: E402
from codedesk.environment import build # noqa: E402
from codedesk.interface import Interface # noqa: E402
FORMAT_ENDS = {"json_plus_text", "no_json", "code_fence", "not_object", "empty", "truncated", "budget"}
def main():
rows = []
for f in sorted(glob.glob(str(ROOT / "reports/calibration-code/*/episodes.jsonl"))):
for line in open(f):
ep = json.loads(line)
if not ep["task_id"].startswith("bugfix-") or "seed" not in ep:
continue
task = build(ep["task_id"], ep["seed"])
try:
tools = task.tools()
iface = Interface(tools, random.Random(f"names:{ep['task_id']}:{ep['seed']}"))
back_tool = {new: old for old, new in iface.tool_names.items()}
back_args = {}
for old, new in zip(tools, iface.tools):
back_args[old.name] = {n: o for o, n in zip(old.args, new.args)}
m = task.m
bad_line = task.original.splitlines()[m["line"] - 1].strip()
mutated_line = (ROOT / "x").name and mutated(task)
edits, ran_tests, read_target = [], 0, False
for t in ep["turns"]:
o = first_json_object(t["reply"]) or {}
tool = back_tool.get(str(o.get("tool", "")).strip())
args = {back_args.get(tool, {}).get(k, k): v for k, v in (o.get("args") or {}).items()}
path = str(args.get("path", "")).lstrip("./")
if tool in ("edit_file", "write_file"):
text = str(args.get("old", "")) + "\n" + str(args.get("content", ""))
edits.append((path, mutated_line in text or bad_line in text, str(args.get("new", ""))))
if tool == "run_tests":
ran_tests += 1
if tool == "read_file" and path == m["file"]:
read_target = True
row = {"task_id": ep["task_id"], "group": task.group, "lib": m["lib"], "level": task.level,
"reward": ep["reward"], "end": ep.get("end"), "turns": len(ep["turns"])}
lib_edits = [e for e in edits if not e[0].startswith(("tests", "test")) and "/test" not in e[0]]
test_edits = [e for e in edits if e not in lib_edits]
if ep["reward"] >= 0.999:
row["status"] = "solved"
elif ep.get("end") in FORMAT_ENDS:
row["status"] = "format ending" + (" after editing the bug's line" if any(e[1] for e in lib_edits) else "")
elif ep.get("end") == "schema_invalid":
row["status"] = "action rejected by the schema"
elif not lib_edits and test_edits:
row["status"] = "edited only tests"
elif not lib_edits:
row["status"] = "no edit to the library" + (" (never opened the buggy file)" if not read_target else
" (opened the buggy file)")
elif any(e[0] == m["file"] and e[1] for e in lib_edits):
row["status"] = "edited the bug's line, fix still wrong"
elif any(e[0] == m["file"] for e in lib_edits):
row["status"] = "edited the right file, wrong place"
else:
row["status"] = "edited a different file"
row["ran_tests"] = ran_tests
rows.append(row)
finally:
task.close()
report(rows)
def mutated(task):
from codedesk.families.bugfix import mutate
return mutate(task.original, task.m).splitlines()[task.m["line"] - 1].strip()
def report(rows):
print(f"bug-fix episodes rebuilt: {len(rows)}\n")
wrong = [r for r in rows if r["status"] != "solved"]
print("All failed episodes:")
for k, v in collections.Counter(r["status"] for r in wrong).most_common():
print(f" {k:55s} {v:4d} {v/len(wrong):.0%}")
print("\nBy group (domain library):")
for g in sorted({r["group"] for r in rows}):
G = [r for r in rows if r["group"] == g]
W = [r for r in G if r["status"] != "solved"]
top = ", ".join(f"{k} {v}" for k, v in collections.Counter(r["status"] for r in W).most_common(3))
print(f" {g:11s} solved {len(G)-len(W)}/{len(G)} {top}")
for lv in (1, 2):
L = [r for r in rows if r["level"] == lv]
if L:
print(f"\nlevel {lv}: solved {sum(r['status']=='solved' for r in L)}/{len(L)}; ran tests in "
f"{sum(r['ran_tests']>0 for r in L)/len(L):.0%} of episodes")
if __name__ == "__main__":
main()
|