Official Codebase of 3DCodeVerse
Python
flywheel: refine rounds as SFT samples for the finetune pipeline
ziyao9a81d5f128b1
diff --git a/harness/bench/refine_sft.py b/harness/bench/refine_sft.pynew file mode 100644index 00000000..921b5082--- /dev/null+++ b/harness/bench/refine_sft.py@@ -0,0 +1,184 @@+"""Refine rounds as SFT samples: the brief the round was given, answered with the code it produced.++``bench/refine_pairs.py`` exports the transition as data (issues, findings, instructions,+diff, delta). This turns the rows that IMPROVED into the message shape the finetune+pipeline consumes (``{"id", "messages": [system, user, assistant]}`` — see+``toolkits/llamafactory/to_llamafactory.py``), so the corpus teaches the one thing the+harness produces and a one-shot dataset cannot: given a judged, gated object and a list of+what is wrong with it, write the corrected files.++ python bench/refine_sft.py bench/out --out refine_sft.jsonl --summary++The assistant turn is the FULL text of every file the round changed, in the same+``=== FILE: path ===`` envelope ``tracks/generation`` parses, because that is the answer+shape a trained model has to produce. Rows are dropped, and counted, when the answer+would not fit ``--max-answer-chars`` or a side cannot be read back out of the run's repo.+"""++from __future__ import annotations++import argparse+import json+import sys+from collections import Counter+from pathlib import Path+from statistics import median++sys.path.insert(0, str(Path(__file__).resolve().parents[1]))++from bench.refine_pairs import Report, iter_rows # noqa: E402+from codeverse.contracts.common import Language # noqa: E402+from codeverse.flywheel._git import GitReadError, read_tree_at # noqa: E402+from codeverse.tracks.prompting import language_system_prompt # noqa: E402+from codeverse.workspace import Workspace # noqa: E402++#: a sample whose answer is longer than this is dropped rather than truncated: half a+#: file teaches a model to write half a file+MAX_ANSWER_CHARS = 120_000+#: outcomes worth training on. A regressed round is a real transition and stays in+#: refine_pairs.jsonl (it is evidence about the loop), but it is not an answer to copy.+DEFAULT_OUTCOMES = ("improved",)+SCHEMA_VERSION = 1+++def envelope(files: dict[str, str]) -> str:+ """The ``=== FILE: path === … === END FILE ===`` blocks ``tracks.generation.parse_multifile``+ reads — the same envelope a single-shot answer must produce, terminator included."""+ return "\n".join(f"=== FILE: {p} ===\n{files[p].rstrip()}\n=== END FILE ===" for p in sorted(files))+++def _bullets(items: list[dict], *keys: str, limit: int = 12) -> list[str]:+ out = []+ for it in items[:limit]:+ parts = [str(it.get(k, "")).strip() for k in keys]+ line = " — ".join(p for p in parts if p)+ if line:+ out.append(f"- {line}")+ return out+++def user_turn(row: dict, before: dict[str, str]) -> str:+ """The brief: what was asked for, what was wrong, and the code as it stood."""+ lines = [f"Refine this {row['track'].replace('_', ' ')} written in {row['language']}.", "",+ "## Request", row["prompt"].strip(), ""]+ if row.get("gate_findings"):+ lines += ["## Gate errors (measured by the harness — fix these first)"]+ lines += _bullets(row["gate_findings"], "target", "message", "fix_hint")+ lines += [""]+ if row.get("issues"):+ lines += ["## What the judge saw"]+ lines += _bullets(row["issues"], "target", "severity", "detail")+ lines += [""]+ if row.get("instruction_lines"):+ lines += ["## Changes to make", *(f"{i}. {ln}" for i, ln in enumerate(row["instruction_lines"], 1)), ""]+ lines += ["## Current files", envelope(before), "",+ "Return every file you change in full, in the same `=== FILE: path ===` envelope."]+ return "\n".join(lines)+++def sample(row: dict, *, max_answer_chars: int = MAX_ANSWER_CHARS) -> dict | None:+ """One SFT row, or ``None`` with the reason recorded on ``row['_drop']``."""+ changed = [f for f in row.get("changed_files") or [] if f.startswith("src/")]+ if not changed:+ row["_drop"] = "no_src_change"+ return None+ ws = Workspace(Path(row["run_dir"]))+ try:+ before_tree = read_tree_at(ws, row["before"]["commit"])+ after_tree = read_tree_at(ws, row["after"]["commit"])+ except (GitReadError, OSError) as e:+ row["_drop"] = f"git_read_failed: {type(e).__name__}"+ return None++ def _text(tree: dict[str, bytes], paths: list[str]) -> dict[str, str] | None:+ out = {}+ for p in paths:+ raw = tree.get(p)+ if raw is None:+ return None+ try:+ out[p] = raw.decode("utf-8")+ except UnicodeDecodeError:+ return None+ return out++ after = _text(after_tree, changed)+ if after is None:+ row["_drop"] = "answer_unreadable"+ return None+ # the brief carries the files the round STARTED from: whatever it changed, plus the+ # rest of src/ it had to stay consistent with+ before = _text(before_tree, sorted(p for p in before_tree if p.startswith("src/"))) or {}+ answer = envelope(after)+ if len(answer) > max_answer_chars:+ row["_drop"] = "answer_too_long"+ return None+ try:+ system = language_system_prompt(Language(row["language"]), tools=False)+ except ValueError:+ row["_drop"] = "unknown_language"+ return None+ return {+ "id": f"{row['battery']}/{row['run']}/r{row['round']:02d}",+ "schema_version": SCHEMA_VERSION,+ "track": row["track"], "language": row["language"], "prompt_id": row["prompt_id"],+ "score_before": row["score_before"], "score_after": row["score_after"],+ "score_delta": row["score_delta"], "changed_files": changed,+ "messages": [{"role": "system", "content": system},+ {"role": "user", "content": user_turn(row, before)},+ {"role": "assistant", "content": answer}],+ }+++def build(root: Path, *, outcomes: tuple[str, ...] = DEFAULT_OUTCOMES,+ max_answer_chars: int = MAX_ANSWER_CHARS) -> tuple[list[dict], Report, Counter]:+ report = Report()+ drops: Counter = Counter()+ out: list[dict] = []+ for row in iter_rows(root, report):+ if row["outcome"] not in outcomes:+ drops[f"outcome:{row['outcome']}"] += 1+ continue+ s = sample(row, max_answer_chars=max_answer_chars)+ if s is None:+ drops[row.get("_drop", "unknown")] += 1+ continue+ out.append(s)+ return out, report, drops+++def main(argv: list[str] | None = None) -> int:+ ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])+ ap.add_argument("root", type=Path, help="a corpus of batteries, one battery, a runs dir or one run")+ ap.add_argument("--out", type=Path, default=None, help="write the samples here (JSONL)")+ ap.add_argument("--outcomes", default=",".join(DEFAULT_OUTCOMES),+ help=f"comma list of refine_pairs outcomes to keep (default {DEFAULT_OUTCOMES[0]})")+ ap.add_argument("--max-answer-chars", type=int, default=MAX_ANSWER_CHARS)+ ap.add_argument("--summary", action="store_true", help="print the counts")+ ns = ap.parse_args(argv)+ outcomes = tuple(x for x in ns.outcomes.split(",") if x)+ rows, report, drops = build(ns.root, outcomes=outcomes, max_answer_chars=ns.max_answer_chars)+ if ns.out:+ ns.out.parent.mkdir(parents=True, exist_ok=True)+ ns.out.write_text("".join(json.dumps(r) + "\n" for r in rows))+ print(f"wrote {ns.out}")+ print(f"{len(rows)} sample(s) from {report.runs} run(s)")+ if ns.summary:+ print(f"rounds seen: {sum(report.kinds.values())} (" ++ ", ".join(f"{k} {v}" for k, v in sorted(report.kinds.items())) + ")")+ if report.drops:+ print("dropped by the scan: " + ", ".join(f"{k} {v}" for k, v in sorted(report.drops.items())))+ if drops:+ print("not exported: " + ", ".join(f"{k} {v}" for k, v in sorted(drops.items())))+ if rows:+ ans = [len(r["messages"][2]["content"]) for r in rows]+ usr = [len(r["messages"][1]["content"]) for r in rows]+ print(f"answer chars: median {int(median(ans))}, max {max(ans)}; "+ f"brief chars: median {int(median(usr))}, max {max(usr)}")+ for field in ("track", "language"):+ print(f"by {field}: " + ", ".join(f"{k} {v}" for k, v in sorted(Counter(r[field] for r in rows).items())))+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/harness/tests/flywheel_cli/test_refine_sft.py b/harness/tests/flywheel_cli/test_refine_sft.pynew file mode 100644index 00000000..79f3f8a2--- /dev/null+++ b/harness/tests/flywheel_cli/test_refine_sft.py@@ -0,0 +1,104 @@+"""Refine rounds as SFT samples (bench/refine_sft.py)."""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from bench.refine_sft import DEFAULT_OUTCOMES, build, envelope, main, sample, user_turn+from codeverse.contracts.artifacts import GateFinding, GateReport, Severity+from codeverse.flywheel.record import load_record+from codeverse.tracks.generation import parse_multifile+from tests.flywheel_cli.conftest import make_fake_run+++def _corpus(root: Path, *, scores: tuple[float, float] = (0.30, 0.80)) -> Path:+ ws, _ = make_fake_run(root / "batt" / "runs", "chair", scores=scores)+ rec = load_record(ws)+ rec.rounds[0].gates = [GateReport(gate="connectivity", passed=False, findings=[GateFinding(+ gate="connectivity", severity=Severity.ERROR, target="Leg",+ message="part 'Leg' is floating", fix_hint="move Leg down 12 mm so it meets Seat")])]+ ws.write_json(ws.record_path, rec)+ return root+++def test_an_improved_round_becomes_a_three_message_sample(tmp_path):+ rows, report, drops = build(_corpus(tmp_path))+ assert len(rows) == 1 and report.runs == 1 and not drops+ s = rows[0]+ assert [m["role"] for m in s["messages"]] == ["system", "user", "assistant"]+ assert s["score_before"] == 0.30 and s["score_after"] == 0.80+ assert s["changed_files"] == ["src/model.py", "src/parts/leg.py"]+ assert s["id"].endswith("/r01") and s["track"] == "static_object" and s["language"] == "blender"+ # the answer is the file the round produced, and it parses back through the envelope+ files = parse_multifile(s["messages"][2]["content"])+ assert set(files) == {"src/model.py", "src/parts/leg.py"} and "size=2.0" in files["src/model.py"]+ assert "bpy" in s["messages"][0]["content"].lower()+++def test_the_brief_carries_what_the_round_was_told_and_the_code_it_started_from(tmp_path):+ rows, _, _ = build(_corpus(tmp_path))+ user = rows[0]["messages"][1]["content"]+ assert "a wooden dining chair" in user+ assert "part 'Leg' is floating" in user and "move Leg down 12 mm" in user # gate finding + fix hint+ assert "thicken the legs" in user # the judge's plan / instruction+ assert "=== FILE: src/model.py ===" in user and "size=1.0" in user # the BEFORE state+ assert "size=2.0" not in user # never the answer+ assert "in full" in user.rsplit("\n", 1)[-1]+++def test_only_improved_rounds_are_exported_and_the_rest_are_counted(tmp_path):+ rows, _, drops = build(_corpus(tmp_path, scores=(0.80, 0.80))) # unchanged+ assert rows == [] and drops == {"outcome:unchanged": 1}+ rows, _, drops = build(_corpus(tmp_path / "b", scores=(0.80, 0.30)))+ assert rows == [] and drops == {"outcome:regressed": 1}+ rows, _, _ = build(_corpus(tmp_path / "c", scores=(0.80, 0.30)), outcomes=("regressed",))+ assert len(rows) == 1 and DEFAULT_OUTCOMES == ("improved",)+++def test_an_answer_over_the_cap_is_dropped_not_truncated(tmp_path):+ rows, _, drops = build(_corpus(tmp_path), max_answer_chars=50)+ assert rows == [] and drops == {"answer_too_long": 1}+++def test_a_row_whose_commit_is_gone_is_counted_not_raised(tmp_path):+ from bench.refine_pairs import Report, iter_rows++ row = next(iter(iter_rows(_corpus(tmp_path), Report())))+ row["after"]["commit"] = "0" * 40+ assert sample(row) is None and row["_drop"].startswith("git_read_failed")+++def test_envelope_and_user_turn_are_pure(tmp_path):+ assert envelope({"b.py": "B", "a.py": "A"}) == (+ "=== FILE: a.py ===\nA\n=== END FILE ===\n=== FILE: b.py ===\nB\n=== END FILE ===")+ assert parse_multifile(envelope({"a.py": "A"})) == {"a.py": "A"}+ row = {"track": "static_object", "language": "blender", "prompt": "a stool",+ "gate_findings": [], "issues": [], "instruction_lines": []}+ text = user_turn(row, {"src/x.py": "code"})+ assert "## Gate errors" not in text and "## What the judge saw" not in text+ assert "## Current files" in text and "a stool" in text+++def test_main_writes_jsonl_and_reports(tmp_path, capsys):+ out = tmp_path / "sft.jsonl"+ assert main([str(_corpus(tmp_path)), "--out", str(out), "--summary"]) == 0+ printed = capsys.readouterr().out+ assert "1 sample(s) from 1 run(s)" in printed and "answer chars: median" in printed+ assert "by language: blender 1" in printed+ rows = [json.loads(x) for x in out.read_text().splitlines()]+ assert len(rows) == 1 and rows[0]["schema_version"] == 1++[email protected]("lang,entry", [("urdf_blender", "src/model.py")])+def test_an_articulated_run_keeps_only_its_changed_src_files(tmp_path, lang, entry):+ from codeverse.contracts.common import Language++ ws, _ = make_fake_run(tmp_path / "batt" / "runs", "cab", language=Language(lang))+ rows, _, _ = build(tmp_path)+ assert len(rows) == 1+ assert rows[0]["changed_files"] == [entry, "src/parts/leg.py"] and rows[0]["language"] == lang+ assert set(parse_multifile(rows[0]["messages"][2]["content"])) == {entry, "src/parts/leg.py"}+ assert "robot.urdf" in rows[0]["messages"][1]["content"] # the brief still shows the whole src/