Skip to content

Official Codebase of 3DCodeVerse

Python

spatial: motion metrics, the census and the render-cache tmp dir use the atomic writers — the motion write had the pid-only tmp name unique_tmp was written to replace

gaoypengfa58b8ceb1a9
diff --git a/harness/codeverse3d/languages/scene_threejs/__init__.py b/harness/codeverse3d/languages/scene_threejs/__init__.py
index ee6cf1c2..8d7549f2 100644
--- a/harness/codeverse3d/languages/scene_threejs/__init__.py
+++ b/harness/codeverse3d/languages/scene_threejs/__init__.py
@@ -753,7 +753,7 @@ class SceneThreeJsRuntime:
probe, shaders, census = run_probe(ws, compile=True, timeout_s=timeout_s)
if census:
- (ws.artifacts / "census.json").write_text(json.dumps(census, indent=1))
+ ws.write_json(ws.artifacts / "census.json", census)
gates_dir = ws.artifacts / "gates"
gates_dir.mkdir(exist_ok=True)
(gates_dir / "scene_probe.json").write_text(probe.model_dump_json(indent=1))
diff --git a/harness/codeverse3d/spatial/probes.py b/harness/codeverse3d/spatial/probes.py
index f0504dbb..d95afef0 100644
--- a/harness/codeverse3d/spatial/probes.py
+++ b/harness/codeverse3d/spatial/probes.py
@@ -10,7 +10,6 @@
from __future__ import annotations
-import json
import time
from typing import Any
@@ -105,8 +104,7 @@ def probe_scene(ws: Workspace, *, timeout_s: float = 60.0) -> SceneProbeResult:
"""Import-only probe: module loads, shape valid, cameras valid, update runs, census."""
report, _, census = run_probe(ws, timeout_s=timeout_s)
if census:
- ws.artifacts.mkdir(parents=True, exist_ok=True)
- (ws.artifacts / "census.json").write_text(json.dumps(census, indent=1))
+ ws.write_json(ws.artifacts / "census.json", census)
return _result(report, census)
diff --git a/harness/codeverse3d/spatial/render.py b/harness/codeverse3d/spatial/render.py
index efc67bdf..60049ef1 100644
--- a/harness/codeverse3d/spatial/render.py
+++ b/harness/codeverse3d/spatial/render.py
@@ -12,9 +12,7 @@ import contextlib
import hashlib
import json
import logging
-import os
import shutil
-import threading
import time
from collections.abc import Sequence
from pathlib import Path
@@ -23,7 +21,7 @@ from typing import Any
from codeverse3d.config import get_settings
from codeverse3d.contracts.artifacts import RENDER_MODES, RenderSet, RenderView
from codeverse3d.conventions import OBJECT_VIEWS, ViewPreset
-from codeverse3d.proc import sha256_file
+from codeverse3d.proc import sha256_file, unique_tmp
from codeverse3d.spatial._render_common import build_sheet, out_directory, view_specs
from codeverse3d.spatial.node import (
OWN_BROWSER_ENV,
@@ -223,7 +221,7 @@ def _run_render(
def _store_in_cache(cache_dir: Path, out_dir: Path, record: dict[str, Any]) -> None:
# per-writer tmp + tolerant rename (the vlm_judge._render_slices shape): a pid-only
# name let two judge threads share a tmp dir and delete each other's half-copied PNGs
- tmp = cache_dir.parent / f".{cache_dir.name}.{os.getpid()}-{threading.get_ident()}.tmp"
+ tmp = unique_tmp(cache_dir)
shutil.rmtree(tmp, ignore_errors=True)
try:
tmp.mkdir(parents=True)
diff --git a/harness/codeverse3d/spatial/render_scene.py b/harness/codeverse3d/spatial/render_scene.py
index ee450bbf..05a1a7d8 100644
--- a/harness/codeverse3d/spatial/render_scene.py
+++ b/harness/codeverse3d/spatial/render_scene.py
@@ -25,7 +25,7 @@ from codeverse3d.config import get_settings
from codeverse3d.contracts.artifacts import RenderSet, RenderView
from codeverse3d.contracts.plan import BBox, CameraPlan
from codeverse3d.conventions import SCENE_VIEWS, ViewPreset
-from codeverse3d.proc import read_json_or_none
+from codeverse3d.proc import read_json_or_none, write_json_atomic
from codeverse3d.spatial._render_common import build_sheet, out_directory, view_specs
from codeverse3d.spatial.node import (
OWN_BROWSER_ENV,
@@ -279,14 +279,10 @@ def _store_motion(out_dir: Path, metrics: dict[str, Any]) -> None:
if not rows:
return
metrics["motion"] = [r.as_dict() for r in rows]
- path = out_dir / "metrics.json"
- tmp = path.with_name(f"{path.name}.{os.getpid()}.tmp")
try:
- tmp.write_text(json.dumps(metrics, indent=1))
- tmp.replace(path)
+ write_json_atomic(out_dir / "metrics.json", metrics)
except OSError as e:
- tmp.unlink(missing_ok=True)
- log.warning("could not write motion into %s: %s", path, e)
+ log.warning("could not write motion into %s: %s", out_dir / "metrics.json", e)
def perf_detail(rs: RenderSet) -> str: