Skip to content

Official Codebase of 3DCodeVerse

Python

scene_assets: the asset checker ships as runtime_js/lib/asset_check.mjs — every node driver lives in runtime_js, and the per-run cache write (and its race test) goes

gaoypeng4b62b4e6edce
diff --git a/harness/codeverse3d/tracks/scene_assets.py b/harness/codeverse3d/tracks/scene_assets.py
index f55e73ce..a19f08e5 100644
--- a/harness/codeverse3d/tracks/scene_assets.py
+++ b/harness/codeverse3d/tracks/scene_assets.py
@@ -37,7 +37,7 @@ from codeverse3d.contracts.spec import Constraints, Spec
from codeverse3d.conventions import OBJECT_VIEWS_QUICK, to_pascal, to_snake
from codeverse3d.languages.scene_threejs import asset_file
from codeverse3d.orchestrator import BudgetExceeded
-from codeverse3d.proc import fan_out, read_json_or_none, write_json_atomic, write_text_atomic
+from codeverse3d.proc import fan_out, read_json_or_none, write_json_atomic
from codeverse3d.prompts import render
from codeverse3d.prompts.catalog import language_prompt, language_text
from codeverse3d.tracks.common import (
@@ -781,67 +781,6 @@ class AssetCheck(BaseModel):
return "\n".join(f"- {e}" for e in (self.errors + self.warnings)[:limit])
-_CHECK_JS = r"""
-import * as THREE from 'three';
-const [, , fileUrl, exportName] = process.argv;
-const out = { ok: false, errors: [], warnings: [], size_m: null, tris: 0, meshes: 0, materials: 0 };
-try {
- const mod = await import(fileUrl);
- const fn = mod[exportName] ?? mod.default;
- if (typeof fn !== 'function') {
- out.errors.push(`missing export: this file must \`export function ${exportName}(THREE, opts = {})\` (found: ${Object.keys(mod).join(', ') || 'nothing'})`);
- } else {
- const g = fn(THREE, {});
- if (!g || !g.isObject3D) {
- out.errors.push(`${exportName}(THREE) must return a THREE.Group / Object3D (got ${Object.prototype.toString.call(g)})`);
- } else {
- const box = new THREE.Box3().setFromObject(g);
- const mats = new Set();
- let bad = 0;
- g.traverse((o) => {
- if (!o.isMesh) return;
- out.meshes += 1;
- mats.add(o.material?.uuid ?? o.material);
- const pos = o.geometry?.attributes?.position;
- if (!pos) return;
- const arr = pos.array;
- for (let i = 0; i < arr.length; i++) if (!Number.isFinite(arr[i])) { bad += 1; break; }
- const idx = o.geometry.index;
- out.tris += ((idx ? idx.count : pos.count) / 3) * (o.isInstancedMesh ? o.count : 1);
- });
- out.materials = mats.size;
- if (bad) out.errors.push(`${bad} mesh(es) have NaN/Infinity vertex positions`);
- if (out.meshes === 0) out.errors.push('the returned group contains no meshes');
- if (box.isEmpty()) out.errors.push('the returned group has an empty bounding box');
- else {
- const s = new THREE.Vector3(); box.getSize(s);
- if (!Number.isFinite(s.x + s.y + s.z)) out.errors.push('the bounding box is not finite');
- else { out.size_m = [+s.x.toFixed(3), +s.y.toFixed(3), +s.z.toFixed(3)]; out.min_y = +box.min.y.toFixed(3); }
- }
- out.tris = Math.round(out.tris);
- }
- }
-} catch (e) {
- out.errors.push(`${e?.name || 'Error'}: ${e?.message || String(e)}`);
- const stack = String(e?.stack || '').split('\n').slice(1, 4).filter((l) => l.includes(fileUrl.replace('file://', '')));
- if (stack.length) out.errors.push('at ' + stack.map((l) => l.trim()).join(' / '));
-}
-out.ok = out.errors.length === 0;
-console.log(JSON.stringify(out));
-"""
-
-
-def _checker_path(ctx: RunContext) -> Path:
- """The checker script lives in the harness cache, never in the workspace."""
- p = Path(ctx.settings.cache_dir) / "scene_asset_check.mjs"
- if not p.is_file() or p.read_text() != _CHECK_JS:
- # run_asset_stage fans the assets out over a thread pool, so every thread
- # used to write the SAME '<cache>/scene_asset_check.mjs.tmp' and the loser's
- # replace() raised FileNotFoundError -- swallowed below into ok=True, ran=False.
- write_text_atomic(p, _CHECK_JS)
- return p
-
-
def check_threejs_asset(ctx: RunContext, rel: str, pascal: str, *, timeout_s: float = 60.0,
expected_size_m: tuple[float, float, float] | None = None) -> AssetCheck:
"""Import ``rel`` in node, call ``build<Pascal>(THREE, {})`` and inspect the group.
@@ -852,9 +791,9 @@ def check_threejs_asset(ctx: RunContext, rel: str, pascal: str, *, timeout_s: fl
if not path.is_file():
return AssetCheck(ok=False, ran=True, fatal=True, errors=[f"{rel} was not written"])
try:
- from codeverse3d.spatial.node import run_node
+ from codeverse3d.spatial.node import run_node, runtime_js_dir
- res = run_node(_checker_path(ctx), [path.resolve().as_uri(), f"build{pascal}"], cwd=ctx.ws.root,
+ res = run_node(runtime_js_dir() / "lib" / "asset_check.mjs", [path.resolve().as_uri(), f"build{pascal}"], cwd=ctx.ws.root,
three_hook=True, timeout_s=timeout_s, check=False)
data = res.last_json
if data is None:
diff --git a/harness/runtime_js/README.md b/harness/runtime_js/README.md
index 15362070..4df570f5 100644
--- a/harness/runtime_js/README.md
+++ b/harness/runtime_js/README.md
@@ -81,6 +81,9 @@ lib/stack.mjs Error → {type,message,file,line,frames} with workspace-r
lib/syntax_check.mjs THE JS syntax check: every file parsed as an ES module in ONE node
(vm.SourceTextModule, never linked or run; `node --check` only for a failing
file's line). CLI for the python lints, findSyntaxError() for export_glb
+lib/asset_check.mjs the scene-asset checker: import one asset module, call build<Pascal>(THREE, {}),
+ measure the group (size, tris, meshes, materials, NaN positions); one JSON line
+ for tracks/scene_assets.check_threejs_asset
lib/browser/*.js page-side ESM (served through /__runtime/): renderer.js (THE WebGLRenderer factory —
sRGB + ACES + PCF shadows + pixel ratio 1, used by the object rig AND the scene host),
camera_fit.js (azimuth/elevation → tight bbox fit; the distance math itself is
diff --git a/harness/runtime_js/lib/asset_check.mjs b/harness/runtime_js/lib/asset_check.mjs
new file mode 100644
index 00000000..005894f7
--- /dev/null
+++ b/harness/runtime_js/lib/asset_check.mjs
@@ -0,0 +1,51 @@
+// The scene-asset checker: import one asset module, call its build<Pascal>(THREE, {}) and
+// measure the group it returns (size, triangles, meshes, materials, NaN positions).
+// node --import lib/resolve_three.mjs lib/asset_check.mjs FILE_URL EXPORT_NAME → one JSON line
+// (tracks/scene_assets.check_threejs_asset: spatial.node.run_node(three_hook=True)).
+
+import * as THREE from 'three';
+const [, , fileUrl, exportName] = process.argv;
+const out = { ok: false, errors: [], warnings: [], size_m: null, tris: 0, meshes: 0, materials: 0 };
+try {
+ const mod = await import(fileUrl);
+ const fn = mod[exportName] ?? mod.default;
+ if (typeof fn !== 'function') {
+ out.errors.push(`missing export: this file must \`export function ${exportName}(THREE, opts = {})\` (found: ${Object.keys(mod).join(', ') || 'nothing'})`);
+ } else {
+ const g = fn(THREE, {});
+ if (!g || !g.isObject3D) {
+ out.errors.push(`${exportName}(THREE) must return a THREE.Group / Object3D (got ${Object.prototype.toString.call(g)})`);
+ } else {
+ const box = new THREE.Box3().setFromObject(g);
+ const mats = new Set();
+ let bad = 0;
+ g.traverse((o) => {
+ if (!o.isMesh) return;
+ out.meshes += 1;
+ mats.add(o.material?.uuid ?? o.material);
+ const pos = o.geometry?.attributes?.position;
+ if (!pos) return;
+ const arr = pos.array;
+ for (let i = 0; i < arr.length; i++) if (!Number.isFinite(arr[i])) { bad += 1; break; }
+ const idx = o.geometry.index;
+ out.tris += ((idx ? idx.count : pos.count) / 3) * (o.isInstancedMesh ? o.count : 1);
+ });
+ out.materials = mats.size;
+ if (bad) out.errors.push(`${bad} mesh(es) have NaN/Infinity vertex positions`);
+ if (out.meshes === 0) out.errors.push('the returned group contains no meshes');
+ if (box.isEmpty()) out.errors.push('the returned group has an empty bounding box');
+ else {
+ const s = new THREE.Vector3(); box.getSize(s);
+ if (!Number.isFinite(s.x + s.y + s.z)) out.errors.push('the bounding box is not finite');
+ else { out.size_m = [+s.x.toFixed(3), +s.y.toFixed(3), +s.z.toFixed(3)]; out.min_y = +box.min.y.toFixed(3); }
+ }
+ out.tris = Math.round(out.tris);
+ }
+ }
+} catch (e) {
+ out.errors.push(`${e?.name || 'Error'}: ${e?.message || String(e)}`);
+ const stack = String(e?.stack || '').split('\n').slice(1, 4).filter((l) => l.includes(fileUrl.replace('file://', '')));
+ if (stack.length) out.errors.push('at ' + stack.map((l) => l.trim()).join(' / '));
+}
+out.ok = out.errors.length === 0;
+console.log(JSON.stringify(out));
diff --git a/harness/tests/orchestrator_tracks/test_scene_asset_checker_race.py b/harness/tests/orchestrator_tracks/test_scene_asset_checker_race.py
deleted file mode 100644
index 29f324f4..00000000
--- a/harness/tests/orchestrator_tracks/test_scene_asset_checker_race.py
+++ /dev/null
@@ -1,49 +0,0 @@
-"""CQ-2: run_asset_stage fans assets out over a thread pool, so _checker_path runs
-concurrently. Every thread wrote the same '<cache>/scene_asset_check.mjs.tmp' and the
-loser's replace() raised FileNotFoundError — swallowed by the broad `except Exception`
-in check_threejs_asset into AssetCheck(ok=True, ran=False). The losing asset was then
-reported as PASSING while the min_y sink, the ASSET_MAX_TRIS budget and the
-single-low-poly-box detector were never computed, and size_m stayed None.
-
-Offline: _checker_path only writes the script, it never launches node.
-"""
-
-from __future__ import annotations
-
-import threading
-from concurrent.futures import ThreadPoolExecutor
-from pathlib import Path
-from types import SimpleNamespace
-
-from codeverse3d.tracks.scene_assets import _CHECK_JS, _checker_path
-
-
-def _ctx(cache: Path) -> SimpleNamespace:
- """_checker_path reads exactly one thing off the context."""
- return SimpleNamespace(settings=SimpleNamespace(cache_dir=str(cache)))
-
-
-def test_concurrent_asset_checks_all_get_the_script(tmp_path: Path) -> None:
- ctx = _ctx(tmp_path / "cache")
- n = 8
- bar = threading.Barrier(n)
-
- def one() -> Path:
- bar.wait() # every thread hits the write at once, as the asset fan-out does
- return _checker_path(ctx)
-
- with ThreadPoolExecutor(n) as ex:
- paths = [f.result() for f in [ex.submit(one) for _ in range(n)]]
-
- assert len({str(p) for p in paths}) == 1
- assert paths[0].read_text() == _CHECK_JS
- assert [p.name for p in paths[0].parent.iterdir()] == ["scene_asset_check.mjs"]
-
-
-def test_the_checker_is_rewritten_when_it_is_stale_and_reused_when_it_is_not(tmp_path: Path) -> None:
- ctx = _ctx(tmp_path / "cache")
- p = _checker_path(ctx)
- p.write_text("// stale\n")
- assert _checker_path(ctx).read_text() == _CHECK_JS
- mtime = p.stat().st_mtime_ns
- assert _checker_path(ctx).stat().st_mtime_ns == mtime # unchanged → not rewritten