Official Codebase of 3DCodeVerse
Python
eval/llm: a torn jsonl line no longer crashes a resume (B13)
gaoypeng75161d26dd4a
diff --git a/eval/llm/_jsonl.py b/eval/llm/_jsonl.pynew file mode 100644index 00000000..aef106c6--- /dev/null+++ b/eval/llm/_jsonl.py@@ -0,0 +1,25 @@+"""The tolerant JSONL reader every resumable stage starts from.++The stages rewrite their ``*.jsonl`` whole at the end; a process killed during that write+leaves a torn last line, and a strict ``json.loads`` per line then crashed every later+``--resume`` of the directory. A row that does not parse was not finished: the stage+simply runs it again."""++from __future__ import annotations++import json+from pathlib import Path+++def read_rows(path: Path) -> list[dict]:+ """Every JSON object line of ``path``, in order; blank and torn lines are skipped."""+ rows = []+ with Path(path).open() as f:+ for line in f:+ try:+ row = json.loads(line)+ except ValueError:+ continue+ if isinstance(row, dict):+ rows.append(row)+ return rowsdiff --git a/eval/llm/execute.py b/eval/llm/execute.pyindex 5f6c87ef..21ac5049 100644--- a/eval/llm/execute.py+++ b/eval/llm/execute.py@@ -7,6 +7,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completedfrom pathlib import Pathfrom . import executors+from ._jsonl import read_rowsfrom .executors import CODE_EXT# Blender and Chromium are heavy; CadQuery/OpenSCAD/glslang are light.@@ -30,9 +31,7 @@ def execute_dir(gen_dir: Path, dialect: str, workers: int | None = None, timeoutout_path = gen_dir / "exec_results.jsonl"prev: dict[str, dict] = {}if resume and out_path.exists():- for l in out_path.open():- r = json.loads(l)- prev[r["id"]] = r+ prev = {r["id"]: r for r in read_rows(out_path)}task_dirs = sorted(p for p in gen_dir.iterdir() if p.is_dir() and (ids is None or p.name in ids))todo = [p for p in task_dirs if p.name not in prev]workers = workers or DEFAULT_WORKERS[dialect]diff --git a/eval/llm/generate.py b/eval/llm/generate.pyindex 44c828c7..df6e9409 100644--- a/eval/llm/generate.py+++ b/eval/llm/generate.py@@ -24,6 +24,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completedfrom pathlib import Pathfrom . import config+from ._jsonl import read_rowsfrom .executors import CODE_EXTfrom .extract import extract, summarizefrom .suites import resolve@@ -161,8 +162,7 @@ def api_generate(backend: str, model: str, rows: list[dict], gen_dir: Path, tempgen_dir.mkdir(parents=True, exist_ok=True)done: dict[str, dict] = {}if resume and (gen_dir / "gens.jsonl").exists():- for l in (gen_dir / "gens.jsonl").open():- r = json.loads(l)+ for r in read_rows(gen_dir / "gens.jsonl"):if not r.get("error"):done[r["id"]] = rtodo = [r for r in rows if r["id"] not in done]diff --git a/eval/llm/judge.py b/eval/llm/judge.pyindex cdd52ba4..51069b6b 100644--- a/eval/llm/judge.py+++ b/eval/llm/judge.py@@ -31,6 +31,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completedfrom pathlib import Pathfrom . import config+from ._jsonl import read_rowsfrom .render_views import VIEWSfrom .suites import load_prompts, resolve@@ -148,7 +149,7 @@ def judge_absolute(run: str, suite: str, jc: JudgeClient, tag: str, workers: intout_path = gen_dir / f"judge_{tag}.jsonl"done = {}if resume and out_path.exists():- done = {json.loads(l)["id"]: json.loads(l) for l in out_path.open() if l.strip()}+ done = {r["id"]: r for r in read_rows(out_path)}done = {k: v for k, v in done.items() if v.get("status") == "OK"}todo = [r for r in rows if r["id"] not in done]recs = list(done.values())diff --git a/eval/llm/tests/test_smoke.py b/eval/llm/tests/test_smoke.pyindex cb112e6a..56e5b25b 100644--- a/eval/llm/tests/test_smoke.py+++ b/eval/llm/tests/test_smoke.py@@ -82,3 +82,14 @@ def test_glsl_executor_compile_only():assert rep["status"] == "OK"rep = executors.glsl.run("void mainImage(out vec4 c, in vec2 f){ c = vec4(1.0) }", d + "/bad", render=False)assert rep["status"] == "FAIL"+++def test_a_torn_results_line_does_not_crash_a_resume():+ """A stage killed while rewriting its jsonl leaves a torn last line; resume re-runs that row."""+ execute = importlib.import_module(f"{PKG}.execute")+ with tempfile.TemporaryDirectory() as d:+ out = Path(d) / "exec_results.jsonl"+ out.write_text('{"id": "a", "status": "OK"}\n\n{"id": "b", "sta')+ res = execute.execute_dir(Path(d), "blender")+ assert [r["id"] for r in res] == ["a"]+ assert out.read_text() == '{"id": "a", "status": "OK"}\n'