Skip to content

Official Codebase of 3DCodeVerse

Python

bench: attribute a scene failure to a layer

ziyao24fabca8a17f
diff --git a/harness/bench/scene_stats.py b/harness/bench/scene_stats.py
new file mode 100644
index 00000000..da0188e4
--- /dev/null
+++ b/harness/bench/scene_stats.py
@@ -0,0 +1,146 @@
+"""Where a scene run failed: plan, assets, layout, assembly, gates or the judge.
+
+The scene track is the least measured one in the harness — no recorded run existed when
+this was written — and "the scene track does not work" is not actionable until the failure
+is attributed to a LAYER. The pipeline is
+``plan -> assets -> env -> layouts -> assemble -> build -> gates -> render -> judge``,
+and each layer leaves its own events and artefacts:
+
+ python bench/scene_stats.py bench/out/scene_baseline
+
+Reports, per battery: how many plans needed a re-ask, what the planner chose for each
+asset (``threejs`` procedural module vs ``blender_glb`` hero built with bpy), how many
+assets needed a repair pass, how many placements each zone produced, which gates failed
+and how often, and what the judge complained about. The point is the LAYER attribution:
+a defect in the plan or in the assets follows any change to the assembly language.
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import statistics
+from collections import Counter
+from pathlib import Path
+
+#: pipeline stages in the order the scene track runs them
+STAGES = ("plan", "skeleton", "assets", "env", "layouts", "assemble", "generate", "build")
+
+
+def runs(root: Path) -> list[tuple[str, dict, list[dict]]]:
+ """``(prompt id, record, events)`` per scene run under ``root``, once per run."""
+ out, seen = [], set()
+ for rec in root.rglob("record.json"):
+ if rec.resolve() in seen or "_assets" in rec.parts or "_cand" in rec.parts:
+ continue
+ seen.add(rec.resolve())
+ try:
+ data = json.loads(rec.read_text())
+ except (OSError, ValueError):
+ continue
+ if (data.get("spec") or {}).get("track") != "scene":
+ continue
+ ev = rec.parent / "events.jsonl"
+ events = []
+ if ev.is_file():
+ for line in ev.read_text().splitlines():
+ try:
+ events.append(json.loads(line))
+ except ValueError:
+ continue
+ out.append((str((data.get("spec") or {}).get("id", rec.parent.name)).split("/")[-1], data, events))
+ return sorted(out)
+
+
+def layers(rows: list[tuple[str, dict, list[dict]]]) -> dict:
+ """One counter per layer, over every run."""
+ plan = Counter()
+ assets = Counter()
+ asset_kind = Counter()
+ tris: list[int] = []
+ placements: list[int] = []
+ gates = Counter()
+ gate_errors = Counter()
+ judge = Counter()
+ scores: list[float] = []
+ for _pid, rec, events in rows:
+ for e in events:
+ kind = e.get("event", "")
+ if kind == "plan.invalid":
+ plan["invalid"] += 1
+ elif kind == "plan.restart":
+ plan["restart"] += 1
+ elif kind == "plan.done":
+ plan["ok"] += 1
+ plan["zones"] += int(e.get("n_zones") or 0)
+ elif kind == "asset.generated":
+ assets["ok" if e.get("ok") else "failed"] += 1
+ assets[str(e.get("strategy") or "?")] += 1
+ if e.get("tris"):
+ tris.append(int(e["tris"]))
+ elif kind == "asset.judge_degraded":
+ assets["judge_degraded"] += 1
+ elif kind == "layout.done":
+ placements.append(int(e.get("placements") or 0))
+ if e.get("reasked"):
+ plan["layout_reask"] += 1
+ elif kind == "render.failed":
+ gates["render_failed"] += 1
+ for a in (rec.get("plan") or {}).get("assets") or []:
+ asset_kind[str(a.get("kind"))] += 1
+ for rnd in rec.get("rounds") or []:
+ for g in rnd.get("gates") or []:
+ name = g.get("gate", "?")
+ gates[name] += 1
+ if not g.get("passed"):
+ gates[f"{name}:FAILED"] += 1
+ for f in g.get("findings") or []:
+ if f.get("severity") == "error":
+ gate_errors[name] += 1
+ j = rnd.get("judgment") or {}
+ if j.get("score") is not None:
+ scores.append(float(j["score"]))
+ for issue in j.get("issues") or []:
+ judge[f"{issue.get('severity', '?')}/{issue.get('kind', '?')}"] += 1
+ return {"plan": plan, "assets": assets, "asset_kind": asset_kind, "tris": tris,
+ "placements": placements, "gates": gates, "gate_errors": gate_errors,
+ "judge": judge, "scores": scores}
+
+
+def report(root: Path) -> str:
+ rows = runs(root)
+ if not rows:
+ return f"no scene runs under {root}"
+ d = layers(rows)
+ out = [f"# {len(rows)} scene run(s) under {root}", ""]
+ scores = d["scores"]
+ if scores:
+ out.append(f"scored rounds: {len(scores)}, median {statistics.median(scores):.3f}, "
+ f"min {min(scores):.3f}, max {max(scores):.3f}")
+ out += ["", "## plan", f" {dict(d['plan'])}",
+ "", "## assets",
+ f" outcomes: {dict(d['assets'])}",
+ f" planner chose: {dict(d['asset_kind'])}"]
+ if d["tris"]:
+ out.append(f" triangles per asset: median {statistics.median(d['tris']):.0f}, max {max(d['tris'])}")
+ if d["placements"]:
+ out.append(f"\n## layout\n placements per zone: median {statistics.median(d['placements']):.0f}, "
+ f"zones {len(d['placements'])}")
+ out += ["", "## gates", "| gate | ran | failed | ERROR findings |", "|---|--:|--:|--:|"]
+ for name in sorted(n for n in d["gates"] if not n.endswith(":FAILED")):
+ out.append(f"| {name} | {d['gates'][name]} | {d['gates'][f'{name}:FAILED']} | {d['gate_errors'][name]} |")
+ if d["judge"]:
+ out += ["", "## judge issues (severity/kind)"]
+ out += [f" {n:3}x {k}" for k, n in d["judge"].most_common(12)]
+ return "\n".join(out)
+
+
+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 battery directory (or any directory holding scene runs)")
+ print(report(ap.parse_args(argv).root))
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/harness/tests/compare_bench/test_session_stats.py b/harness/tests/compare_bench/test_session_stats.py
index 4c3571c4..a80fd72a 100644
--- a/harness/tests/compare_bench/test_session_stats.py
+++ b/harness/tests/compare_bench/test_session_stats.py
@@ -189,3 +189,42 @@ def test_the_two_penetration_probes_are_compared_not_merged(tmp_path: Path) -> N
assert c["sweep_only"] == 1 # rod|frame: the sparse draw never saw it
assert c["examples"] == ["b/p: knob|post"]
assert corroborate([rec], 0.005)["both"] == 0 # nothing that deep on the sparse side
+
+
+def test_scene_stats_attributes_a_failure_to_a_layer(tmp_path: Path) -> None:
+ """"The scene track does not work" is not actionable until the failure is attributed to
+ a layer: a defect in the plan or in the assets follows any change to the assembly
+ language, a defect in assembly does not."""
+ from bench.scene_stats import layers, report, runs
+
+ run = tmp_path / "runs" / "scn_easy_garden"
+ run.mkdir(parents=True)
+ (run / "record.json").write_text(json.dumps({
+ "spec": {"id": "scenes_v1/scn_easy_garden", "track": "scene"},
+ "plan": {"assets": [{"name": "Lantern", "kind": "threejs"},
+ {"name": "Bridge", "kind": "blender_glb"}]},
+ "rounds": [{"index": 0, "gates": [
+ {"gate": "scene_placement", "passed": False, "findings": [
+ {"severity": "error", "message": "Lantern floats 0.4 m"}]},
+ {"gate": "scene_frames", "passed": True, "findings": []}],
+ "judgment": {"score": 0.3, "issues": [{"severity": "critical", "kind": "layout"}]}}],
+ }))
+ (run / "events.jsonl").write_text("\n".join(json.dumps(e) for e in [
+ {"t": 1, "event": "plan.invalid", "attempt": 0},
+ {"t": 2, "event": "plan.done", "n_zones": 3},
+ {"t": 3, "event": "asset.generated", "asset": "Lantern", "ok": True, "strategy": "single-shot", "tris": 1200},
+ {"t": 4, "event": "asset.generated", "asset": "Bridge", "ok": False, "strategy": "single-shot+repair"},
+ {"t": 5, "event": "layout.done", "zone": "Court", "placements": 6, "reasked": True},
+ ]))
+
+ assert len(runs(tmp_path)) == 1
+ d = layers(runs(tmp_path))
+ assert d["plan"]["invalid"] == 1 and d["plan"]["zones"] == 3 and d["plan"]["layout_reask"] == 1
+ assert d["assets"]["ok"] == 1 and d["assets"]["failed"] == 1
+ assert dict(d["asset_kind"]) == {"threejs": 1, "blender_glb": 1}
+ assert d["gates"]["scene_placement:FAILED"] == 1 and d["gate_errors"]["scene_placement"] == 1
+ assert d["gates"]["scene_frames:FAILED"] == 0
+ assert d["judge"]["critical/layout"] == 1
+
+ text = report(tmp_path)
+ assert "| scene_placement | 1 | 1 | 1 |" in text and "'threejs': 1" in text