Official Codebase of 3DCodeVerse
Python
cli: flush the summary before exiting — the real cause of "driver output lost"
ziyaoc411740b2c77
diff --git a/harness/docs/DECISIONS.md b/harness/docs/DECISIONS.mdindex e560378b..28ff6409 100644--- a/harness/docs/DECISIONS.md+++ b/harness/docs/DECISIONS.md@@ -938,6 +938,44 @@ written) that were accepted because the code works that way and the tests pin itretried once itself. Rare (1 cell of 12 in each of two arms) and expensive when itfires.+* **D61 A driver's summary is flushed before it exits (2026-09-06).** `lib/cli.finish()`+ wrote the JSON summary and then called `process.exit()`. Node's stdout to a PIPE is+ asynchronous and `process.exit` does not flush it, so any summary larger than the pipe+ buffer was cut mid-JSON and the caller saw no parsable last line. Measured on the+ starter scene, `probe_scene.mjs --compile`: to a file the summary is **10 462 bytes and+ parses**, through a pipe it was **exactly 8192 and did not**.++ **This is the real mechanism behind every "driver output lost" and "[?] scene did not+ boot" in the scene batteries, and D54 attributed them to the wrong cause.** They do not+ depend on how busy the machine is — they depend on how big the census is, which is why+ they read as weather across two batteries. desert_canyon spent three repair attempts on+ it in `scene_baseline`; `scene_textures/japanese_garden` spent three more and lost the+ texture use that arm existed to measure. The retries D54 added are still right (they+ cost nothing and cover a genuinely transient loss) but they could never fix this one:+ a deterministic truncation reproduces on every attempt.++ `finish()` now sets `process.exitCode` and exits from the write's completion callback;+ the watchdog goes through it too. Pinned by a driver whose summary is 64 KiB.++* **D62 The `inside_mesh_bbox` term was NOT narrowed, and here is the counter-example+ (2026-09-06).** Over the 113 recorded camera checks of `scene_baseline` the ray term+ (`nearest < 0.3 m`) never fired once and all ten firings came from the bbox term with the+ lens 1.3-10.0 m clear of anything — sparse scattered fields (`Drift`, `FoliageMass`,+ `Midges`) whose box spans the scene while their geometry is nowhere near the eye. A+ candidate rule ("the bbox flag counts only when a ray also lands within 1 m, or when no+ ray lands at all") took those ten firings to zero and added none, so it was written and+ validated against the corpus.++ **It was then reverted, because the corpus could not see what it broke.**+ `test_a_lens_inside_geometry_retreats_until_clear` puts a lens inside a `BarCounter`+ whose material is FrontSide: the rays are culled by the box itself, escape, and land on+ the floor 1.2 m away — so `nearest` is neither null nor under 1 m, and a genuinely buried+ camera stops being repaired. The recorded corpus contains no buried camera at all, which+ is exactly why validating on it was not enough. The measurement stands and is worth+ redoing with a discriminator that asks the right question (how far the eye is from the+ CONTAINING mesh's own surface, not from anything at all); the rule does not ship on this+ evidence.+## Rejected / deferred* A versioned `Spec`/`RunRecord`/`RunState` load-normaliser (rejected 2026-08-30: of the sevendiff --git a/harness/runtime_js/lib/cli.mjs b/harness/runtime_js/lib/cli.mjsindex 7d5660cd..ddef386f 100644--- a/harness/runtime_js/lib/cli.mjs+++ b/harness/runtime_js/lib/cli.mjs@@ -25,10 +25,23 @@ export function envFlag(name, fallback) {return fallback;}-/** Print the final JSON summary as the LAST stdout line and exit. */+/**+ * Print the final JSON summary as the LAST stdout line and exit.+ *+ * `process.exit()` does NOT flush a pending stdout write when stdout is a PIPE — node's+ * stdout is asynchronous there — so a summary larger than the pipe buffer is cut mid-JSON+ * and the caller sees no parsable last line. Measured 2026-09-06 on the starter scene:+ * to a file the summary is 10 462 bytes and parses; through a pipe it is **exactly 8192**+ * and does not. That is the real mechanism behind every "driver output lost" /+ * "[?] scene did not boot" in `bench/out/scene_baseline` and `scene_textures` — it depends+ * on the census size, not on how busy the box is, which is why it looked like weather.+ *+ * Write with a completion callback and exit from it; `exitCode` is set first so that a+ * process with nothing else pending still ends with the right code.+ */export function finish(summary, code = 0) {- process.stdout.write(JSON.stringify(summary) + '\n');- process.exit(code);+ process.exitCode = code;+ process.stdout.write(JSON.stringify(summary) + '\n', () => process.exit(code));}/** Fail loudly with a JSON summary line (code 2 = could not run). */@@ -77,8 +90,7 @@ export function armWatchdog(ms, onFire) {const t = setTimeout(() => {try { onFire && onFire(); } catch (e) { /* ignore */ }process.stderr.write(`error: driver exceeded ${ms} ms budget\n`);- process.stdout.write(JSON.stringify({ ok: false, error: `timeout after ${ms} ms` }) + '\n');- process.exit(3);+ finish({ ok: false, error: `timeout after ${ms} ms` }, 3); // flushes; see finish()}, ms);t.unref();return t;diff --git a/harness/tests/scene_runtime/test_camera_volumetric.py b/harness/tests/scene_runtime/test_camera_volumetric.pyindex d74b99fa..4556b52a 100644--- a/harness/tests/scene_runtime/test_camera_volumetric.py+++ b/harness/tests/scene_runtime/test_camera_volumetric.py@@ -121,3 +121,4 @@ def test_a_camera_buried_in_a_solid_slab_is_still_retreated():"""The control: the same geometry that writes depth is matter, and the lens moves."""out = run_node_json(REPAIR_JS.replace("DEPTH_WRITE", "true").replace("'./lib/", f"'{RUNTIME_JS}/lib/"))assert out["fix"], "a lens inside a solid slab must still be repaired"+diff --git a/harness/tests/threejs_render/test_cli_flush.py b/harness/tests/threejs_render/test_cli_flush.pynew file mode 100644index 00000000..2161972a--- /dev/null+++ b/harness/tests/threejs_render/test_cli_flush.py@@ -0,0 +1,50 @@+"""A driver's summary survives a pipe, however big it is.++`finish()` used to `process.stdout.write(...)` and then `process.exit()`. Node's stdout+to a PIPE is asynchronous, and `process.exit` does not flush it, so a summary larger than+the pipe buffer was cut mid-JSON and the caller saw no parsable last line.++Measured 2026-09-06 on the starter scene, `probe_scene.mjs --compile`: written to a file+the summary is 10 462 bytes and parses; through a pipe it was **exactly 8192** and did not.+That is the real mechanism behind every "driver output lost" and "[?] scene did not boot"+in `bench/out/scene_baseline` / `scene_textures` — it depends on how big the census is, not+on how busy the machine is, which is why it read as weather for two batteries. In+`scene_textures/japanese_garden` it cost the round all three repair attempts and the+texture use the arm existed to measure.+"""++from __future__ import annotations++import json++import pytest++from codeverse.spatial.node import run_node, runtime_js_dir+from tests.scene_runtime.conftest import needs_node++pytestmark = [pytest.mark.node, needs_node]++#: comfortably past the 8 KiB pipe buffer that used to truncate+PAYLOAD_BYTES = 64 * 1024+++def _driver(tmp_path, n: int, code: int):+ p = tmp_path / "d.mjs"+ p.write_text(f"""+import {{ finish }} from {json.dumps(str(runtime_js_dir() / 'lib' / 'cli.mjs'))};+finish({{ ok: true, blob: 'x'.repeat({n}), tail: 'LAST' }}, {code});+""")+ return p++[email protected]("code", [0, 1])+def test_a_large_summary_survives_the_pipe(tmp_path, code: int):+ res = run_node(_driver(tmp_path, PAYLOAD_BYTES, code), [], timeout_s=60, check=False)+ assert res.rc == code+ assert res.last_json is not None, "the summary was truncated mid-JSON"+ assert res.last_json["tail"] == "LAST" and len(res.last_json["blob"]) == PAYLOAD_BYTES+++def test_a_small_summary_still_works(tmp_path):+ res = run_node(_driver(tmp_path, 10, 0), [], timeout_s=60, check=False)+ assert res.rc == 0 and res.last_json["tail"] == "LAST"