3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Send only the changed table bytes of a native video frame (#1861)
Nguyen Xuan Ane3d0cfb6a69d
diff --git a/.github/workflows/cook-addon.yml b/.github/workflows/cook-addon.ymlindex 178f4e62..7ebf013d 100644--- a/.github/workflows/cook-addon.yml+++ b/.github/workflows/cook-addon.yml@@ -52,6 +52,7 @@ on:- 'scripts/make-spark-spec.mjs'- 'scripts/native-bake-page.js'- 'scripts/native-capacity.mjs'+ - 'scripts/native-export-delta.js'- 'scripts/native-png.mjs'- 'scripts/native-post-look.mjs'- 'scripts/render-engine.mjs'@@ -368,6 +369,7 @@ on:- 'tests/native-dispatch-determinism.test.js'- 'tests/native-dispersion.test.js'- 'tests/native-exit-code.test.js'+ - 'tests/native-export-delta.test.js'- 'tests/native-filter.test.js'- 'tests/native-glass-albedo.test.js'- 'tests/native-grid-volumes.test.js'diff --git a/changes/1861.md b/changes/1861.mdnew file mode 100644index 00000000..fa49a1e7--- /dev/null+++ b/changes/1861.md@@ -0,0 +1 @@+- A native video frame after the first reads only the scene table bytes that changed since the frame before, so a scene with moving particles no longer moves its static meshes from the page every frame; the video is the same bytes (`JS67_NATIVE_EXPORT_DELTA=off` reads every table whole).diff --git a/docs/agent-render-jobs.md b/docs/agent-render-jobs.mdindex 3d13b4fe..79c90cf6 100644--- a/docs/agent-render-jobs.md+++ b/docs/agent-render-jobs.md@@ -190,7 +190,7 @@ An `image`, `stills` or `video` job can ask for the offline [path tracer](https:"renderer": { "type": "pathTracer", "backend": "gpu", "samples": 64, "bounces": 8, "denoise": true, "seed": 0, "maxSeconds": 120 } }```-Every field after `type` is optional. `backend` is `cpu` (the default, byte-identical on SwiftShader smokes), `gpu`, which traces in a WebGL2 shader and needs a runtime started on the GPU backend (a `--backend cpu` runtime refuses it), or `native`, which traces on hardware ray queries (Vulkan) through the optional `@codepawlhq/cook` package. A native job exports the scene from the job page, traces it in the job runner with the Cycles-matching kernel (`JS67_NATIVE_KERNEL=legacy` keeps the older one), then denoises (OIDN runs on a device kept alive between frames when the OIDN library is installed, so only the first frame pays for the device and the filter commit; `metrics.pathTracer.oidnMode` says `in-process` or `cli`) and writes the PNG as the other backends do: `denoise: true` means OIDN with the albedo and normal guides when `@codepawlhq/crucible-denoise-oidn` and its binary are installed, else (or when OIDN fails) the GPU a-trous filter of the native tracer with a note in `metrics.pathTracer.denoiserNote` (`denoiser: "atrous"`; it is biased, it smooths; `denoise: "wavelet"` asks for the CPU filter). The report carries `kernel` and `averageSamples`. The Cycles-matching kernel takes up to 4,000,000 unique triangles (copies of a shared mesh count once, and the scene may hold four times that with every copy counted); the job reads the scene from its page as one Blob over the DevTools stream (about half the time of returning it as base64, and no single message bounds it); the `cpu` backend and the older kernel keep 400,000, and the `gpu` backend takes what the page's WebGL2 context holds, up to 2,000,000 (see [path-tracer.md](https://github.com/codepawl/crucible/blob/main/docs/path-tracer.md)). A larger scene is refused with an error that names the limit. On the Cycles-matching kernel the job sends each material whole (FORMAT-v2 tables, instanced or not): blend opacity (`alphaMode: "blend"` with `opacity` below 1), sheen, `specular` and `specularColor`, anisotropy, iridescence, attenuation, and the clearcoat, transmission, sheen, anisotropy and coat normal maps all reach the kernel, and `metrics.pathTracer.ignored` lists only what it does not shade (the legacy kernel keeps the v1 tables and the old notes). A scene whose materials cannot be exported whole is traced from the v1 tables for that frame with the reason in `materialsReason`; the native tracer stays usable. It never fails for lack of the package: without it, without its built binary or without a Vulkan ray query adapter the job runs on `gpu` (`cpu` on a cpu runtime) and `metrics.pathTracer.backendNote` says why; `metrics.pathTracer.backend` always names what ran. The look up costs one import and is cached per process. Native traces volume fog and subsurface scattering with the same walk as the WebGPU megakernel, honours `maxSeconds`, and honours `noiseThreshold` on the GPU (after 16 samples, and then every 8, a converged pixel draws no more; `report.averageSamples` is the mean a pixel drew). The `gpu` fallback ignores `noiseThreshold`. The report in `metrics.pathTracer` names the `backend` that ran, the `gpu`, `gpuCompileSeconds` and `gpuKernelCompiles`. A `stills` or `video` job traces every frame with the same options: the kernel compiles once per job page and each frame reuses it, so a film shows `gpuKernelCompiles: 1`. A native `stills` or `video` job (the Cycles-matching kernel) also keeps one native session for the whole job: a frame that only moves the camera reuses the uploaded scene, the compiled kernel and the acceleration structures; changed materials, lights, instance transforms or triangles (the table may change length) are sent to the session, which rebuilds only the changed meshes' acceleration structures; a frame whose record counts, placed meshes, emissive meshes, textures or environment changed gets a new session, and a failed session update or render falls back to a new session or a one-shot trace for that frame. `metrics.pathTracer.nativeSession` says which one each frame took (`path` is `new-session`, `reused`, `updated` or `one-shot`, with `reason`, `changedTables`, `changedMeshes` (after a triangles update), `pipelineReused` and `accelerationReused`); `metrics.nativeSessionRoutes` lists the `path` of every frame. A job reuses the session only when the tables the page exports are equal to the previous frame's apart from the camera (`reused`), or differ in material values, lights, instance transforms or triangles (`updated`). Geometry that moves with the time, such as particle systems (sparks, swaying hair) or a moved node in a scene exported as flattened triangles, goes through `updated` and costs one BLAS build per changed mesh plus the TLAS. A changed emissive mesh, other record counts or another set of placed meshes gives that frame a new session, which costs what a one-shot trace costs and is byte-identical to it; `reason` names the first table that differed. A single-frame job and `JS67_NATIVE_KERNEL=legacy` trace one-shot and carry no `nativeSession`; `JS67_NATIVE_SESSIONS=off` in the server's environment traces every frame one-shot (default on). The session is closed when the job ends or fails, and every frame of a stills job equals the one-shot render byte for byte. A native video also blends its frames over time, so the noise the denoiser leaves does not shimmer from frame to frame: each pixel finds where its surface point was in the frame before (from the first-hit distance and the two cameras) and the filtered light is averaged with what was gathered there. A pixel whose surface was hidden, off screen, another surface or seen from a direction more than about 6 degrees away in the frame before starts again, so a cut to another view starts again too, and the whole image starts again at a new shot of a `timeline` and on any frame whose scene, light or render settings changed (its route is not `reused`), so a video with moving parts or lights, or a dip through black, gets no blending on those frames. Each frame of such a video traces with its own seed (`seed` plus the frame number), and `metrics.pathTracer.temporal` reports `historyShare` (the share of pixels that found their history, from the last frame) with `restarted` saying why an image started again. The blend assumes a still scene under a moving camera: light that depends on the view (a sharp reflection, a highlight, the view through glass) trails the camera by a few frames, and an orthographic camera is not blended. `JS67_NATIVE_TEMPORAL=off` in the server's environment turns the blending off, which makes every video frame equal its one-shot render again. A video of traced frames goes through the ffmpeg encoder (the browser H.264 encoder has no drawn frame to read), `maxSeconds` limits each frame, and the job deadline is `maxSeconds` times the frame count plus 60 seconds, up to the job ceiling. Limits: `samples` 1 to 1024, `bounces` 1 to 16, `seed` 0 to 2147483647, `maxSeconds` 1 to 600 (the job's deadline is `maxSeconds` plus 60 seconds; a 1920 by 1080 still of a 180,000 triangle room at 64 samples took 246 s, so give a large still `maxSeconds` 600). `passes` are refused with it, because the tracer has no object ID, depth or normal pass; part findings (framing, floating, intersecting, textures, z-fighting) still reach `metrics.diagnostics` from the first frame, with a `findings-unavailable` entry saying that exposure findings are not available on a traced job (see [mcp.md](mcp.md)). The tracer loads in the job page on demand, uses eight threads at below-normal priority, stops pixels that have converged (`report.averageSamples`), and writes `render.png`; `scene_render_result` metrics carry `pathTracer` with the samples, threads, seconds and what the scene asked for that was not drawn. The same recipe and seed give the same PNG on the cpu and gpu backends; the native Cycles-matching kernel is not always byte-identical between runs (on the demo room at 960 by 540 and 256 samples two jobs differed in a few bytes; a 480 by 270 job with OIDN did not). A job that passes `maxSeconds` fails and writes no image.+Every field after `type` is optional. `backend` is `cpu` (the default, byte-identical on SwiftShader smokes), `gpu`, which traces in a WebGL2 shader and needs a runtime started on the GPU backend (a `--backend cpu` runtime refuses it), or `native`, which traces on hardware ray queries (Vulkan) through the optional `@codepawlhq/cook` package. A native job exports the scene from the job page, traces it in the job runner with the Cycles-matching kernel (`JS67_NATIVE_KERNEL=legacy` keeps the older one), then denoises (OIDN runs on a device kept alive between frames when the OIDN library is installed, so only the first frame pays for the device and the filter commit; `metrics.pathTracer.oidnMode` says `in-process` or `cli`) and writes the PNG as the other backends do: `denoise: true` means OIDN with the albedo and normal guides when `@codepawlhq/crucible-denoise-oidn` and its binary are installed, else (or when OIDN fails) the GPU a-trous filter of the native tracer with a note in `metrics.pathTracer.denoiserNote` (`denoiser: "atrous"`; it is biased, it smooths; `denoise: "wavelet"` asks for the CPU filter). The report carries `kernel` and `averageSamples`. The Cycles-matching kernel takes up to 4,000,000 unique triangles (copies of a shared mesh count once, and the scene may hold four times that with every copy counted); the job reads the scene from its page as one Blob over the DevTools stream (about half the time of returning it as base64, and no single message bounds it); from the second frame of a `stills` or `video` job the page sends only the table bytes that changed since the frame before (a scene with moving particles sends the particles, not the static meshes), the job rebuilds the same tables from them, and `metrics.pathTracer.exportSentBytes` says how many bytes crossed (`JS67_NATIVE_EXPORT_DELTA=off` reads every table whole); the `cpu` backend and the older kernel keep 400,000, and the `gpu` backend takes what the page's WebGL2 context holds, up to 2,000,000 (see [path-tracer.md](https://github.com/codepawl/crucible/blob/main/docs/path-tracer.md)). A larger scene is refused with an error that names the limit. On the Cycles-matching kernel the job sends each material whole (FORMAT-v2 tables, instanced or not): blend opacity (`alphaMode: "blend"` with `opacity` below 1), sheen, `specular` and `specularColor`, anisotropy, iridescence, attenuation, and the clearcoat, transmission, sheen, anisotropy and coat normal maps all reach the kernel, and `metrics.pathTracer.ignored` lists only what it does not shade (the legacy kernel keeps the v1 tables and the old notes). A scene whose materials cannot be exported whole is traced from the v1 tables for that frame with the reason in `materialsReason`; the native tracer stays usable. It never fails for lack of the package: without it, without its built binary or without a Vulkan ray query adapter the job runs on `gpu` (`cpu` on a cpu runtime) and `metrics.pathTracer.backendNote` says why; `metrics.pathTracer.backend` always names what ran. The look up costs one import and is cached per process. Native traces volume fog and subsurface scattering with the same walk as the WebGPU megakernel, honours `maxSeconds`, and honours `noiseThreshold` on the GPU (after 16 samples, and then every 8, a converged pixel draws no more; `report.averageSamples` is the mean a pixel drew). The `gpu` fallback ignores `noiseThreshold`. The report in `metrics.pathTracer` names the `backend` that ran, the `gpu`, `gpuCompileSeconds` and `gpuKernelCompiles`. A `stills` or `video` job traces every frame with the same options: the kernel compiles once per job page and each frame reuses it, so a film shows `gpuKernelCompiles: 1`. A native `stills` or `video` job (the Cycles-matching kernel) also keeps one native session for the whole job: a frame that only moves the camera reuses the uploaded scene, the compiled kernel and the acceleration structures; changed materials, lights, instance transforms or triangles (the table may change length) are sent to the session, which rebuilds only the changed meshes' acceleration structures; a frame whose record counts, placed meshes, emissive meshes, textures or environment changed gets a new session, and a failed session update or render falls back to a new session or a one-shot trace for that frame. `metrics.pathTracer.nativeSession` says which one each frame took (`path` is `new-session`, `reused`, `updated` or `one-shot`, with `reason`, `changedTables`, `changedMeshes` (after a triangles update), `pipelineReused` and `accelerationReused`); `metrics.nativeSessionRoutes` lists the `path` of every frame. A job reuses the session only when the tables the page exports are equal to the previous frame's apart from the camera (`reused`), or differ in material values, lights, instance transforms or triangles (`updated`). Geometry that moves with the time, such as particle systems (sparks, swaying hair) or a moved node in a scene exported as flattened triangles, goes through `updated` and costs one BLAS build per changed mesh plus the TLAS. A changed emissive mesh, other record counts or another set of placed meshes gives that frame a new session, which costs what a one-shot trace costs and is byte-identical to it; `reason` names the first table that differed. A single-frame job and `JS67_NATIVE_KERNEL=legacy` trace one-shot and carry no `nativeSession`; `JS67_NATIVE_SESSIONS=off` in the server's environment traces every frame one-shot (default on). The session is closed when the job ends or fails, and every frame of a stills job equals the one-shot render byte for byte. A native video also blends its frames over time, so the noise the denoiser leaves does not shimmer from frame to frame: each pixel finds where its surface point was in the frame before (from the first-hit distance and the two cameras) and the filtered light is averaged with what was gathered there. A pixel whose surface was hidden, off screen, another surface or seen from a direction more than about 6 degrees away in the frame before starts again, so a cut to another view starts again too, and the whole image starts again at a new shot of a `timeline` and on any frame whose scene, light or render settings changed (its route is not `reused`), so a video with moving parts or lights, or a dip through black, gets no blending on those frames. Each frame of such a video traces with its own seed (`seed` plus the frame number), and `metrics.pathTracer.temporal` reports `historyShare` (the share of pixels that found their history, from the last frame) with `restarted` saying why an image started again. The blend assumes a still scene under a moving camera: light that depends on the view (a sharp reflection, a highlight, the view through glass) trails the camera by a few frames, and an orthographic camera is not blended. `JS67_NATIVE_TEMPORAL=off` in the server's environment turns the blending off, which makes every video frame equal its one-shot render again. A video of traced frames goes through the ffmpeg encoder (the browser H.264 encoder has no drawn frame to read), `maxSeconds` limits each frame, and the job deadline is `maxSeconds` times the frame count plus 60 seconds, up to the job ceiling. Limits: `samples` 1 to 1024, `bounces` 1 to 16, `seed` 0 to 2147483647, `maxSeconds` 1 to 600 (the job's deadline is `maxSeconds` plus 60 seconds; a 1920 by 1080 still of a 180,000 triangle room at 64 samples took 246 s, so give a large still `maxSeconds` 600). `passes` are refused with it, because the tracer has no object ID, depth or normal pass; part findings (framing, floating, intersecting, textures, z-fighting) still reach `metrics.diagnostics` from the first frame, with a `findings-unavailable` entry saying that exposure findings are not available on a traced job (see [mcp.md](mcp.md)). The tracer loads in the job page on demand, uses eight threads at below-normal priority, stops pixels that have converged (`report.averageSamples`), and writes `render.png`; `scene_render_result` metrics carry `pathTracer` with the samples, threads, seconds and what the scene asked for that was not drawn. The same recipe and seed give the same PNG on the cpu and gpu backends; the native Cycles-matching kernel is not always byte-identical between runs (on the demo room at 960 by 540 and 256 samples two jobs differed in a few bytes; a 480 by 270 job with OIDN did not). A job that passes `maxSeconds` fails and writes no image.## Controller APIdiff --git a/scripts/agent-native-trace.mjs b/scripts/agent-native-trace.mjsindex 0af1925d..e5e292da 100644--- a/scripts/agent-native-trace.mjs+++ b/scripts/agent-native-trace.mjs@@ -7,6 +7,7 @@ import { pathToFileURL } from 'node:url';import { clampLimitsByEngine, requireRenderEngine } from './render-engine.mjs';import { estimateRelativeNoise } from '../src/path-tracer-denoise.js';import { readBlobBytes } from './cdp-blob-read.mjs';+import { applyExportDelta } from './native-export-delta.js';import { requireExportFitsCapacity } from './native-capacity.mjs';import { mapLookToCookPost } from './native-post-look.mjs';import { encodeOpaquePng } from './native-png.mjs';@@ -136,15 +137,27 @@ function readChunkedManifest(manifest, capacity, caller) {* Reads every table of a chunked export (the `manifest` of splitExportForChunks) from a page that holds them as one Blob, back to* back in manifest order, and returns the export with its `tables` as bytes, which traceFrameNative takes like the base64 of a* one-message export. `readBlob(byteLength)` resolves with all of its bytes; each table is copied out into a buffer of its own.- * No single string holds a table, so the scene is bounded by memory, not by a message.+ * No single string holds a table, so the scene is bounded by memory, not by a message. A manifest with `chunks.delta` sends only the+ * ranges that changed since the export `baseline` (`{ id, tables }`) holds; the tables come back patched, byte for byte what a full+ * export gives, and `sentBytes` says how many bytes crossed.*/-export async function collectExportBlob(manifest, readBlob, { capacity } = {}) {+export async function collectExportBlob(manifest, readBlob, { capacity, baseline } = {}) {const { chunks, exported, capacityUse } = readChunkedManifest(manifest, capacity, 'collectExportBlob');- const totalBytes = Object.values(chunks.tables).reduce((total, byteLength) => total + byteLength, 0);+ const { delta } = chunks;+ if (delta && baseline?.id !== delta.base) {+ throw new RangeError(`The native scene export is a delta against ${JSON.stringify(delta.base)}, and the baseline held here is ${JSON.stringify(baseline?.id)}. Export the scene again without a baseline.`);+ }+ const totalBytes = delta+ ? Object.values(delta.ranges).reduce((total, list) => total + list.reduce((sum, [, length]) => sum + length, 0), 0)+ : Object.values(chunks.tables).reduce((total, byteLength) => total + byteLength, 0);const blob = await readBlob(totalBytes);if (blob.length !== totalBytes) {throw new RangeError(`The native scene export sent ${blob.length} bytes, expected ${totalBytes}. The page changed the export while it was read.`);}+ if (delta) {+ const tables = applyExportDelta({ previous: baseline.tables, lengths: chunks.tables, ranges: delta.ranges, sent: blob });+ return { ...exported, tables, ...(capacityUse ? { capacityUse } : {}), sentBytes: totalBytes };+ }const tables = {};let offset = 0;for (const [name, byteLength] of Object.entries(chunks.tables)) {@@ -159,17 +172,28 @@ export async function collectExportBlob(manifest, readBlob, { capacity } = {}) {* `exportNativeScene` takes. The page holds the tables as one Blob and the job runner reads it over the DevTools stream of* `session` (the CDP session of the page; one is made and detached here when none is passed). The page lets go of its copy* when the read ends, whether it succeeded or not. `capacity` (from readNativeCapacity) makes a scene the adapter cannot hold- * fail before its tables are read.+ * fail before its tables are read. `baseline` is an object the caller keeps for the frames of one job: each frame after the first+ * reads only the table ranges that changed since the one before, unless JS67_NATIVE_EXPORT_DELTA=off. A frame that cannot use it+ * (the page lost its copy) reads every table, and the tables are the same bytes either way.*/-export async function fetchNativeExport(page, input, { capacity, session } = {}) {+export async function fetchNativeExport(page, input, { capacity, session, baseline } = {}) {const maximumTriangles = chunkedTriangleBound(capacity?.maxUniqueTriangles);- const manifest = await page.evaluate(request => window.__agentPreview.exportNativeScene(request), { ...input, chunked: true, maximumTriangles });- return readHeldNativeExport(page, manifest, {+ const keepBaseline = baseline !== undefined && process.env.JS67_NATIVE_EXPORT_DELTA !== 'off';+ const request = { ...input, chunked: true, maximumTriangles, ...(keepBaseline ? { keepBaseline, ...(baseline.id === undefined ? {} : { deltaBase: baseline.id }) } : {}) };+ const manifest = await page.evaluate(exportRequest => window.__agentPreview.exportNativeScene(exportRequest), request);+ const { exportId } = manifest;+ const exported = await readHeldNativeExport(page, manifest, {blobExpression: 'window.__agentPreview.nativeExportBlob()',release: () => window.__agentPreview.releaseNativeExport(),capacity,session,+ baseline,});+ if (baseline !== undefined) {+ baseline.id = keepBaseline ? exportId : undefined;+ baseline.tables = keepBaseline && exportId !== undefined ? exported.tables : undefined;+ }+ return exported;}/**@@ -178,10 +202,10 @@ export async function fetchNativeExport(page, input, { capacity, session } = {})* whether it succeeded or not. The Blob comes over the DevTools stream of `session` (made and detached here when none is* passed); `capacity` fails a scene the adapter cannot hold before any byte is read.*/-export async function readHeldNativeExport(page, manifest, { blobExpression, release, capacity, session: givenSession }) {+export async function readHeldNativeExport(page, manifest, { blobExpression, release, capacity, session: givenSession, baseline }) {const session = givenSession ?? await page.context().newCDPSession(page);try {- return await collectExportBlob(manifest, byteLength => readBlobBytes(session, blobExpression, byteLength), { capacity });+ return await collectExportBlob(manifest, byteLength => readBlobBytes(session, blobExpression, byteLength), { capacity, baseline });} finally {await page.evaluate(release).catch(() => {});if (!givenSession) await session.detach().catch(() => {});@@ -407,7 +431,7 @@ async function traceOneFrameNative({ tracer, exported, width, height, samples, bkernel, averageSamples, noiseThreshold, ...(estimatedNoise === null ? {} : { estimatedNoise }), triangles: exported.triangles, inputBytes: tableBytes(tables), ...(exported.instances === undefined ? {} : { instances: exported.instances }),...(exported.instancing === undefined ? {} : { instancing: exported.instancing, instancingReason: exported.instancingReason }),...(exported.materialsReason === undefined ? {} : { materialsReason: exported.materialsReason }), tracedSeconds, ignored: exported.ignored,- ...(route ? { nativeSession: route } : {}), ...(exported.capacityUse ? { nativeCapacity: exported.capacityUse } : {}) };+ ...(route ? { nativeSession: route } : {}), ...(exported.capacityUse ? { nativeCapacity: exported.capacityUse } : {}), ...(exported.sentBytes === undefined ? {} : { exportSentBytes: exported.sentBytes }) };const rawLinear = features.color;let linear = features.color;const denoiseStarted = performance.now();diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.jsindex fba61aa2..ff8e4c95 100644--- a/scripts/agent-preview-browser.js+++ b/scripts/agent-preview-browser.js@@ -1,7 +1,13 @@import { exportNativeSceneTables, nativeTexelTracerIfAvailable, splitExportForChunks } from './native-bake-page.js';+import { DELTA_MAXIMUM_BASELINE_BYTES, deltaPieces, planExportDelta } from './native-export-delta.js';// The byte tables of the last chunked native export, held until the job runner has read them (releaseNativeExport).let heldNativeExportTables = null;+// The ranges of those tables the job runner still lacks (a delta against its baseline), or null when it reads them whole.+let heldNativeExportRanges = null;+// The tables of the last export a job asked to keep, so the next frame can send only what changed since (#1861).+let nativeExportBaseline = null;+let nativeExportCount = 0;import { buildWithKernels, kernelWorkersAvailable, kernelStats, resetKernelStats } from '../src/kernel-offload.js';import { createAgentScene, applyCameraFields, loadAllAgentSceneModules, allAgentSceneModulesLoaded, needsMeshBooleans, loadMeshBooleans, meshBooleansLoaded, setManifoldModuleLoader, needsPhysics, needsBuoyancy, loadPhysicsEngine, physicsEngineLoaded, setPhysicsModuleLoader } from '../src/agent-scene.js';import { CrucibleRenderer } from '../src/renderer.js';@@ -1097,7 +1103,7 @@ window.__agentPreview = {// The scene at one instant packed into the tables the native ray query tracer reads (renderer.backend "native"). The job// runner traces them outside the page; each table comes back as base64 bytes. With `chunked` the answer holds a `chunks`// manifest instead of the tables, which stay here for nativeExportBlob, so no one message bounds the scene.- async exportNativeScene({ sessionId, animation, motionTime, options, chunked = false, maximumTriangles }) {+ async exportNativeScene({ sessionId, animation, motionTime, options, chunked = false, maximumTriangles, deltaBase, keepBaseline = false }) {const record = requireSession(sessionId);if (record.asset && (animation !== undefined || motionTime !== undefined)) {record.scene.sampleFrame({ ...(animation === undefined ? {} : { animation }),@@ -1114,6 +1120,9 @@ window.__agentPreview = {// The "cycles" kernel gets FORMAT-v2 tables with whole materials, and copies of one geometry stay instances; the legacy kernel reads flattened v1 triangles.const camera = createCamera(record.scene.camera, options.width, options.height);heldNativeExportTables = null;+ heldNativeExportRanges = null;+ const baseline = nativeExportBaseline;+ nativeExportBaseline = null;const packed = exportNativeSceneTables({ parts: record.scene.parts, environment, camera, options: packOptions, kernel, instancing: instancing === true,transport: chunked ? 'chunks' : 'message', maximumTriangles,modules: { buildTraceScene, resolveTexture, buildNativeInput, buildJobNativeInput, buildFlattenedJobNativeInput, pack, gridVolumeInput } });@@ -1122,6 +1131,17 @@ window.__agentPreview = {const split = splitExportForChunks(packed);heldNativeExportTables = split.tables;exported = split.manifest;+ // With the baseline the job runner holds, send the ranges that changed since it. Any other baseline (a reloaded page, a+ // job that asked for none) sends every table whole, so a frame never depends on a copy that is not there.+ if (deltaBase !== undefined && baseline && baseline.id === deltaBase) {+ heldNativeExportRanges = planExportDelta(baseline.tables, split.tables);+ exported = { ...exported, chunks: { ...exported.chunks, delta: { base: deltaBase, ranges: heldNativeExportRanges } } };+ }+ const keptBytes = Object.values(split.tables).reduce((total, bytes) => total + bytes.byteLength, 0);+ if (keepBaseline && keptBytes <= DELTA_MAXIMUM_BASELINE_BYTES) {+ nativeExportBaseline = { id: `export-${++nativeExportCount}`, tables: split.tables };+ exported = { ...exported, exportId: nativeExportBaseline.id };+ }}return { ...exported, ignored: describeIgnoredFeatures(record.scene.parts, environment, 'cpu', { kernelMaterials: exported.wholeMaterials, clipStart: camera.clipStart }) };},@@ -1131,11 +1151,12 @@ window.__agentPreview = {if (!heldNativeExportTables) {throw new Error('nativeExportBlob: no chunked native export is held. Call exportNativeScene with chunked: true first.');}- return new Blob(Object.values(heldNativeExportTables));+ return new Blob(heldNativeExportRanges ? deltaPieces(heldNativeExportTables, heldNativeExportRanges) : Object.values(heldNativeExportTables));},// Lets go of the held export once the job runner has every table.releaseNativeExport() {heldNativeExportTables = null;+ heldNativeExportRanges = null;},// The PNG of linear pixels (three floats each, base64) that a denoiser outside the page returned, or of the wavelet// filter over the kept features when none came back.diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjsindex a6a70d34..2b1a1a8a 100644--- a/scripts/agent-runtime.mjs+++ b/scripts/agent-runtime.mjs@@ -806,6 +806,8 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browlet frameReceiver;// A stills or video job of native frames renders them all through one session; it is closed when the job ends.let nativeSequence;+ // The tables of the last native export of this job, so a frame reads only the byte ranges that changed since (#1861).+ const nativeExportBaseline = {};// A native video job blends each frame into the ones before it, so the noise the denoiser leaves does not shimmer.let nativeTemporal;let nativeTemporalShotIndex;@@ -943,7 +945,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browconst blendsOverTime = usesSession && settings.kind === 'video' && chooseNativeKernel() === 'cycles' && chooseNativeTemporal();const frameSeed = blendsOverTime ? temporalFrameSeed(tracerOptions.seed, frame) : tracerOptions.seed;try {- const exported = await fetchNativeExport(jobPage, { ...frameInput, options: { width, height, bounces: tracerOptions.bounces, seed: frameSeed, fireflyClamp: 20, noiseThreshold: tracerOptions.noiseThreshold, samples: tracerOptions.samples, instancing: chooseNativeInstancing(), kernel: chooseNativeKernel() } }, { capacity: readNativeCapacity(trace.tracer).capacity, session });+ const exported = await fetchNativeExport(jobPage, { ...frameInput, options: { width, height, bounces: tracerOptions.bounces, seed: frameSeed, fireflyClamp: 20, noiseThreshold: tracerOptions.noiseThreshold, samples: tracerOptions.samples, instancing: chooseNativeInstancing(), kernel: chooseNativeKernel() } }, { capacity: readNativeCapacity(trace.tracer).capacity, session, baseline: usesSession ? nativeExportBaseline : undefined });exportedScene = true;if (usesSession) nativeSequence ??= createNativeSequence({ tracer: trace.tracer });if (blendsOverTime) {diff --git a/scripts/native-export-delta.js b/scripts/native-export-delta.jsnew file mode 100644index 00000000..54c15996--- /dev/null+++ b/scripts/native-export-delta.js@@ -0,0 +1,138 @@+// The tables of a native video frame, sent as what changed since the frame before (#1861).+//+// The page packs the whole scene every frame, and almost all of the bytes come out the same: the static meshes, the lights, the+// environment. The page keeps the tables of its last export, finds the byte ranges that differ and puts only those in the Blob+// the job runner reads. The job runner holds the tables it got last time and patches them, so it ends with the same bytes a full+// export gives. This file is shared by the page (planning) and the job runner (patching); it touches no browser or Node API.++/** Two changed words closer than this many bytes share one range, so a particle tail is a few ranges instead of thousands. */+export const DELTA_GAP_BYTES = 1024;+/** A table that changes in more places than this goes whole. */+export const DELTA_MAXIMUM_RANGES = 4096;+/** The page keeps its last tables only when they are no bigger than this, so a huge scene is not held twice. */+export const DELTA_MAXIMUM_BASELINE_BYTES = 256 * 1024 ** 2;++const WORD_BYTES = 4;++/**+ * The byte ranges `[offset, length]` in which `after` differs from `before` (two Uint8Arrays of the same length), in order.+ * Ranges merge across gaps up to DELTA_GAP_BYTES. Returns null when there are more than DELTA_MAXIMUM_RANGES of them.+ */+export function changedRanges(before, after) {+ const wordCount = after.byteLength >> 2;+ const aligned = before.byteOffset % WORD_BYTES === 0 && after.byteOffset % WORD_BYTES === 0;+ const ranges = [];+ let rangeStart = -1;+ let rangeEnd = 0;+ const closeRange = () => {+ if (rangeStart >= 0) ranges.push([rangeStart, rangeEnd - rangeStart]);+ };+ const noteChange = (offset, length) => {+ if (rangeStart >= 0 && offset - rangeEnd <= DELTA_GAP_BYTES) {+ rangeEnd = offset + length;+ return true;+ }+ closeRange();+ rangeStart = offset;+ rangeEnd = offset + length;+ return ranges.length <= DELTA_MAXIMUM_RANGES;+ };+ if (aligned) {+ const beforeWords = new Uint32Array(before.buffer, before.byteOffset, wordCount);+ const afterWords = new Uint32Array(after.buffer, after.byteOffset, wordCount);+ for (let word = 0; word < wordCount; word++) {+ if (beforeWords[word] !== afterWords[word] && !noteChange(word * WORD_BYTES, WORD_BYTES)) return null;+ }+ } else {+ for (let byte = 0; byte < wordCount * WORD_BYTES; byte++) {+ if (before[byte] !== after[byte] && !noteChange(byte, 1)) return null;+ }+ }+ for (let byte = wordCount * WORD_BYTES; byte < after.byteLength; byte++) {+ if (before[byte] !== after[byte] && !noteChange(byte, 1)) return null;+ }+ closeRange();+ return ranges.length > DELTA_MAXIMUM_RANGES ? null : ranges;+}++/**+ * What to send of `tables` (name to Uint8Array) given the `previous` ones: name to a list of `[offset, length]` ranges. A table+ * with no previous one, another length or too many changes is one range over all of it; an unchanged table is an empty list.+ */+export function planExportDelta(previous, tables) {+ const ranges = {};+ for (const [name, bytes] of Object.entries(tables)) {+ const before = previous[name];+ const changed = before && before.byteLength === bytes.byteLength ? changedRanges(before, bytes) : null;+ ranges[name] = changed ?? [[0, bytes.byteLength]];+ }+ return ranges;+}++/** The bytes a plan sends, as views of `tables` in manifest order, ready for `new Blob(pieces)`. */+export function deltaPieces(tables, ranges) {+ const pieces = [];+ for (const [name, bytes] of Object.entries(tables)) {+ for (const [offset, length] of ranges[name]) pieces.push(bytes.subarray(offset, offset + length));+ }+ return pieces;+}++/** How many bytes a plan sends. */+export function deltaByteLength(ranges) {+ let total = 0;+ for (const list of Object.values(ranges)) {+ for (const [, length] of list) total += length;+ }+ return total;+}++function requireRangesInside(name, list, byteLength) {+ let previousEnd = 0;+ for (const range of list) {+ const [offset, length] = Array.isArray(range) ? range : [];+ if (!Number.isInteger(offset) || !Number.isInteger(length) || offset < previousEnd || length < 0 || offset + length > byteLength) {+ throw new RangeError(`The native scene export changed table "${name}" at ${JSON.stringify(range)}, outside its ${byteLength} bytes or out of order. Export the scene again without a baseline.`);+ }+ previousEnd = offset + length;+ }+}++/**+ * The full tables from the bytes a delta sent. `lengths` is the byte length of every table now, `ranges` what was sent, `sent` the+ * bytes of all ranges back to back, and `previous` the tables of the export the delta was made against. A table that changed gets a+ * buffer of its own (the previous one may still be held by a native session); a table that did not is the previous object.+ */+export function applyExportDelta({ previous, lengths, ranges, sent }) {+ const tables = {};+ let cursor = 0;+ for (const [name, byteLength] of Object.entries(lengths)) {+ const list = ranges[name];+ if (!Array.isArray(list)) throw new TypeError(`The native scene export delta has no ranges for table "${name}".`);+ requireRangesInside(name, list, byteLength);+ const wholeTable = list.length === 1 && list[0][0] === 0 && list[0][1] === byteLength;+ const before = previous?.[name];+ if (list.length === 0 && before?.byteLength === byteLength) {+ tables[name] = before;+ continue;+ }+ if (wholeTable) {+ tables[name] = sent.slice(cursor, cursor + byteLength);+ cursor += byteLength;+ continue;+ }+ if (before?.byteLength !== byteLength) {+ throw new RangeError(`The native scene export sent only part of table "${name}", but the tables held here have ${before ? before.byteLength : 'no'} bytes for it, not ${byteLength}. Export the scene again without a baseline.`);+ }+ const patched = before.slice();+ for (const [offset, length] of list) {+ patched.set(sent.subarray(cursor, cursor + length), offset);+ cursor += length;+ }+ tables[name] = patched;+ }+ if (cursor !== sent.byteLength) {+ throw new RangeError(`The native scene export delta sent ${sent.byteLength} bytes, and its ranges hold ${cursor}. The page changed the export while it was read.`);+ }+ return tables;+}diff --git a/tests/native-export-delta.test.js b/tests/native-export-delta.test.jsnew file mode 100644index 00000000..468ceca9--- /dev/null+++ b/tests/native-export-delta.test.js@@ -0,0 +1,256 @@+import assert from 'node:assert/strict';+import { createHash } from 'node:crypto';+import { existsSync } from 'node:fs';+import { copyFile, mkdtemp, readFile, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import test from 'node:test';+import { setTimeout as delay } from 'node:timers/promises';+import { Client } from '@modelcontextprotocol/client';+import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';++import { collectExportBlob, fetchNativeExport } from '../scripts/agent-native-trace.mjs';+import {+ DELTA_GAP_BYTES, DELTA_MAXIMUM_RANGES, applyExportDelta, changedRanges, deltaByteLength, deltaPieces, planExportDelta,+} from '../scripts/native-export-delta.js';+import { cookInstalled } from './support/cook.js';++function patternBytes(length, seed) {+ const bytes = new Uint8Array(length);+ for (let index = 0; index < length; index++) bytes[index] = (index * 31 + seed) & 255;+ return bytes;+}++function withChanges(bytes, changes) {+ const copy = bytes.slice();+ for (const [offset, value] of changes) copy[offset] = value;+ return copy;+}++const concatenate = pieces => Uint8Array.from(pieces.flatMap(piece => [...piece]));++test('changedRanges lists where two tables differ and merges changes closer than the gap', () => {+ const before = patternBytes(40000, 1);+ const after = withChanges(before, [[100, 7], [101, 9], [100 + DELTA_GAP_BYTES, 3], [20000, 1], [39999, 5]]);+ assert.deepEqual(changedRanges(before, before), []);+ const ranges = changedRanges(before, after);+ assert.equal(ranges.length, 3);+ assert.equal(ranges[0][0], 100 - (100 % 4));+ assert.ok(ranges[0][0] + ranges[0][1] > 100 + DELTA_GAP_BYTES, 'two changes within the gap are one range');+ assert.ok(ranges[2][0] + ranges[2][1] === 40000, 'a change in the last byte is covered');+});++test('changedRanges reads a table that starts off a word boundary and has a tail shorter than a word', () => {+ const backing = patternBytes(1003, 4);+ const before = backing.subarray(1, 1002);+ const after = withChanges(before, [[3, 1], [1000, 8]]);+ const ranges = changedRanges(before, after);+ assert.deepEqual(ranges.map(([offset, length]) => [offset <= 3 && offset + length > 3, offset <= 1000 && offset + length > 1000]).flat().filter(Boolean).length, 2);+});++test('changedRanges gives up with null past the range cap, so the table goes whole', () => {+ const before = new Uint8Array((DELTA_MAXIMUM_RANGES + 5) * (DELTA_GAP_BYTES + 8));+ const after = before.slice();+ for (let index = 0; index < DELTA_MAXIMUM_RANGES + 5; index++) after[index * (DELTA_GAP_BYTES + 8)] = 1;+ assert.equal(changedRanges(before, after), null);+ assert.deepEqual(planExportDelta({ triangles: before }, { triangles: after }), { triangles: [[0, after.byteLength]] });+});++test('a delta rebuilds the same tables a full export gives, and leaves the previous ones alone', () => {+ const previous = { triangles: patternBytes(30000, 1), materials: patternBytes(512, 2), lights: patternBytes(128, 3), environment: patternBytes(900, 4) };+ const tables = {+ triangles: withChanges(previous.triangles, [[29990, 1], [29991, 2], [5000, 9]]),+ materials: previous.materials.slice(),+ lights: patternBytes(130, 3),+ environment: withChanges(previous.environment, [[0, 1]]),+ };+ const frozenPrevious = structuredClone(previous);+ const ranges = planExportDelta(previous, tables);+ assert.deepEqual(ranges.materials, []);+ assert.deepEqual(ranges.lights, [[0, 130]], 'a table of another length goes whole');+ assert.ok(deltaByteLength(ranges) < 3000, `only the changes cross, not ${deltaByteLength(ranges)} bytes`);+ const sent = concatenate(deltaPieces(tables, ranges));+ assert.equal(sent.byteLength, deltaByteLength(ranges));+ const lengths = Object.fromEntries(Object.entries(tables).map(([name, bytes]) => [name, bytes.byteLength]));+ const rebuilt = applyExportDelta({ previous, lengths, ranges, sent });+ assert.deepEqual(rebuilt, tables);+ assert.equal(rebuilt.materials, previous.materials, 'an unchanged table is the object that is already held');+ assert.notEqual(rebuilt.triangles.buffer, previous.triangles.buffer, 'a changed table is a new buffer, since a native session may still hold the old one');+ assert.deepEqual(previous, frozenPrevious);+});++test('applyExportDelta refuses ranges outside a table, a short blob and a table it does not hold', () => {+ const previous = { triangles: patternBytes(16, 1) };+ const lengths = { triangles: 16 };+ assert.throws(() => applyExportDelta({ previous, lengths, ranges: { triangles: [[12, 8]] }, sent: new Uint8Array(8) }), /outside its 16 bytes/);+ assert.throws(() => applyExportDelta({ previous, lengths, ranges: { triangles: [[0, 4], [2, 4]] }, sent: new Uint8Array(8) }), /out of order/);+ assert.throws(() => applyExportDelta({ previous, lengths, ranges: { triangles: [[0, 4]] }, sent: new Uint8Array(3) }), /sent 3 bytes|hold/);+ assert.throws(() => applyExportDelta({ previous: {}, lengths, ranges: { triangles: [[0, 4]] }, sent: new Uint8Array(4) }), /no bytes for it/);+ assert.throws(() => applyExportDelta({ previous, lengths, ranges: {}, sent: new Uint8Array(0) }), /no ranges for table "triangles"/);+});++function deltaManifest(previous, tables, base) {+ const ranges = planExportDelta(previous, tables);+ const lengths = Object.fromEntries(Object.entries(tables).map(([name, bytes]) => [name, bytes.byteLength]));+ return { manifest: { schemaVersion: 2, counts: {}, exportId: 'export-2', chunks: { tables: lengths, chunkBytes: 16, delta: { base, ranges } } }, sent: concatenate(deltaPieces(tables, ranges)) };+}++test('collectExportBlob reads a delta against its baseline and says how many bytes crossed', async () => {+ const previous = { triangles: patternBytes(20000, 1), materials: patternBytes(64, 2) };+ const tables = { triangles: withChanges(previous.triangles, [[19000, 4]]), materials: previous.materials.slice() };+ const { manifest, sent } = deltaManifest(previous, tables, 'export-1');+ const collected = await collectExportBlob(manifest, async byteLength => {+ assert.equal(byteLength, sent.byteLength);+ return sent;+ }, { baseline: { id: 'export-1', tables: previous } });+ assert.deepEqual(collected.tables, tables);+ assert.equal(collected.sentBytes, sent.byteLength);+ assert.ok(collected.sentBytes < 100);+ await assert.rejects(collectExportBlob(structuredClone(manifest), async () => sent, { baseline: { id: 'export-0', tables: previous } }), /delta against "export-1".*"export-0"/);+ await assert.rejects(collectExportBlob(structuredClone(manifest), async () => sent), /delta against "export-1"/);+});++// A page that answers exportNativeScene the way the real one does: a delta against the baseline id it is asked for, else whole.+function fakePage(frames) {+ const requests = [];+ let frameIndex = 0;+ let blob = null;+ return {+ requests,+ async evaluate(run, argument) {+ if (argument === undefined) return undefined;+ requests.push(argument);+ const tables = frames[frameIndex];+ const previousTables = frameIndex > 0 ? frames[frameIndex - 1] : null;+ const lengths = Object.fromEntries(Object.entries(tables).map(([name, bytes]) => [name, bytes.byteLength]));+ const chunks = { tables: lengths, chunkBytes: 16 };+ const baseId = frameIndex > 0 ? `export-${frameIndex}` : undefined;+ let pieces = Object.values(tables);+ if (argument.deltaBase !== undefined && argument.deltaBase === baseId) {+ const ranges = planExportDelta(previousTables, tables);+ chunks.delta = { base: baseId, ranges };+ pieces = deltaPieces(tables, ranges);+ }+ blob = concatenate(pieces);+ frameIndex += 1;+ return { schemaVersion: 2, counts: {}, chunks, ...(argument.keepBaseline ? { exportId: `export-${frameIndex}` } : {}) };+ },+ context: () => ({ newCDPSession: async () => { throw new Error('the test passes a session'); } }),+ blobBytes: () => blob,+ };+}++function sessionOver(page) {+ return {+ async send(method, parameters) {+ if (method === 'Runtime.evaluate') return { result: { objectId: 'object-1' } };+ if (method === 'IO.resolveBlob') return { uuid: 'uuid' };+ if (method === 'IO.read') {+ const slice = page.blobBytes().subarray(parameters.offset, parameters.offset + parameters.size);+ return { base64Encoded: true, data: Buffer.from(slice).toString('base64'), eof: false };+ }+ return {};+ },+ };+}++test('fetchNativeExport keeps a baseline across frames and reads only what changed', async () => {+ const first = { triangles: patternBytes(50000, 1), materials: patternBytes(64, 2) };+ const second = { triangles: withChanges(first.triangles, [[49000, 3]]), materials: first.materials.slice() };+ const third = { triangles: withChanges(second.triangles, [[49004, 5]]), materials: first.materials.slice() };+ const page = fakePage([first, second, third]);+ const session = sessionOver(page);+ const baseline = {};+ const frameOne = await fetchNativeExport(page, { options: {} }, { session, baseline });+ assert.equal(frameOne.sentBytes, undefined, 'the first frame has no baseline and reads every table');+ assert.equal(baseline.id, 'export-1');+ const frameTwo = await fetchNativeExport(page, { options: {} }, { session, baseline });+ const frameThree = await fetchNativeExport(page, { options: {} }, { session, baseline });+ assert.deepEqual(frameTwo.tables, second);+ assert.deepEqual(frameThree.tables, third);+ assert.ok(frameTwo.sentBytes < 100 && frameThree.sentBytes < 100);+ assert.deepEqual(page.requests.map(request => [request.keepBaseline, request.deltaBase]), [[true, undefined], [true, 'export-1'], [true, 'export-2']]);+ assert.equal(baseline.id, 'export-3');+});++test('fetchNativeExport asks for no baseline when none is given or JS67_NATIVE_EXPORT_DELTA is off', async t => {+ const tables = { triangles: patternBytes(64, 1) };+ const withoutBaseline = fakePage([tables]);+ await fetchNativeExport(withoutBaseline, { options: {} }, { session: sessionOver(withoutBaseline) });+ assert.equal(withoutBaseline.requests[0].keepBaseline, undefined);+ process.env.JS67_NATIVE_EXPORT_DELTA = 'off';+ t.after(() => { delete process.env.JS67_NATIVE_EXPORT_DELTA; });+ const switchedOff = fakePage([tables, tables]);+ const baseline = { id: 'stale', tables };+ await fetchNativeExport(switchedOff, { options: {} }, { session: sessionOver(switchedOff), baseline });+ assert.equal(switchedOff.requests[0].keepBaseline, undefined);+ assert.equal(switchedOff.requests[0].deltaBase, undefined);+ assert.equal(baseline.id, undefined, 'a switched-off job holds no baseline');+});++// ---- A real clip, rendered with and without the delta (needs the built MCP server, Chrome and the native addon) ----++const serverEntry = new URL('../bin/crucible-mcp.mjs', import.meta.url).pathname.replace(/^\/(\w:)/, '$1');+const hasBuild = existsSync(new URL('../build/mcp/server.mjs', import.meta.url));+const chromePath = process.env.CHROME_PATH ?? 'C:/Program Files/Google/Chrome/Application/chrome.exe';+const hasBrowser = existsSync(chromePath) || Boolean(process.env.CI);++function particleRecipe() {+ const nodes = [{ id: 'ground', geometry: 'floor', material: 'stone', translation: [0, -0.1, -1], scale: [12, 0.2, 12] }];+ for (let index = 0; index < 4; index++) nodes.push({ id: `ball-${index}`, geometry: 'ball', material: 'stone', scale: [0.5, 0.5, 0.5], translation: [index * 1.6 - 2.4, 0.6, -1] });+ nodes.push({ id: 'dust', geometry: 'speck', material: 'ember' });+ return { format: 'crucible.scene', schemaVersion: 1,+ geometries: [{ id: 'floor', type: 'box', bevel: 0 }, { id: 'ball', type: 'sphere', segments: 16 }, { id: 'speck', type: 'box', bevel: 0 }],+ materials: [{ id: 'stone', color: [0.6, 0.55, 0.5] }, { id: 'ember', color: [1, 0.6, 0.2] }], nodes,+ particles: { systems: [{ node: 'dust', seed: 1, count: 200, lifetime: 3, speed: 1, size: 0.03 }] },+ environment: { setup: 'sunset' }, camera: { eye: [0, 3, 6], target: [0, 0.5, -1], fov: 50 } };+}++async function renderClip(workDirectory, label, deltaSwitch) {+ const transport = new StdioClientTransport({+ command: process.execPath,+ args: [serverEntry, '--backend', 'cpu', '--memory', '768', '--jobs', join(workDirectory, `jobs-${label}`)],+ cwd: workDirectory,+ env: { ...process.env, JS67_NATIVE_EXPORT_DELTA: deltaSwitch },+ stderr: 'ignore',+ });+ const client = new Client({ name: 'native-export-delta-test', version: '1.0.0' });+ await client.connect(transport);+ try {+ const call = (name, toolArguments) => client.callTool({ name, arguments: toolArguments }, { timeout: 600000 });+ const created = (await call('scene_create', { recipe: particleRecipe() })).structuredContent;+ const settings = { kind: 'video', width: 64, height: 64, fps: 6, duration: 4 / 6, motion: { start: 0 },+ renderer: { type: 'pathTracer', backend: 'native', samples: 2, bounces: 2, denoise: false, seed: 1, maxSeconds: 120 } };+ const submitted = await call('scene_render_submit', { sessionId: created.sessionId, expectedRevision: created.revision, settings });+ assert.notEqual(submitted.isError, true, submitted.content?.[0]?.text);+ const { jobId } = submitted.structuredContent;+ let record;+ for (;;) {+ record = (await call('scene_render_status', { jobId })).structuredContent;+ if (['completed', 'cancelled', 'failed'].includes(record.state)) break;+ await delay(200);+ }+ assert.equal(record.state, 'completed', record.error);+ const result = (await call('scene_render_result', { jobId, response_format: 'detailed' })).structuredContent;+ const videoPath = join(workDirectory, `${label}.mp4`);+ await copyFile(result.artifacts[0].path, videoPath);+ return { video: await readFile(videoPath), pathTracer: (result.metrics ?? record.metrics).pathTracer };+ } finally {+ await client.close();+ }+}++test('a native clip with moving particles is byte for byte the same video with the delta on and off',+ { skip: (!cookInstalled && 'Cook is not installed') || (!hasBuild && 'run npm run build:lib first') || (!hasBrowser && 'no Chrome'), timeout: 900000 }, async context => {+ const workDirectory = await mkdtemp(join(tmpdir(), 'crucible-export-delta-'));+ context.after(() => rm(workDirectory, { recursive: true, force: true }));+ const full = await renderClip(workDirectory, 'full', 'off');+ const delta = await renderClip(workDirectory, 'delta', 'on');+ if (full.pathTracer.backend !== 'native') {+ context.skip(`this machine has no native tracer (${full.pathTracer.backendNote})`);+ return;+ }+ assert.equal(full.pathTracer.exportSentBytes, undefined, 'with the delta off every frame reads whole tables');+ assert.ok(delta.pathTracer.exportSentBytes < delta.pathTracer.inputBytes / 2, `the last frame sent ${delta.pathTracer.exportSentBytes} of ${delta.pathTracer.inputBytes} bytes`);+ assert.equal(createHash('sha256').update(delta.video).digest('hex'), createHash('sha256').update(full.video).digest('hex'));+ });