3D engine that AI agents drive
Give every edit of a baked scene the bake deadline
Nguyen Xuan Andf15eba14866
diff --git a/docs/light-bake.md b/docs/light-bake.mdindex 4b19b0a2..7be76884 100644--- a/docs/light-bake.md+++ b/docs/light-bake.md@@ -28,7 +28,7 @@ const renderer = new Renderer67(canvas, bakedParts, rendererOptions);| `standard` (default) | 128 | 2 | 6 || `fine` | 192 | 2 | 7 |-Ranges are 1 to 256 samples, 1 to 3 bounces and 1 to 12 texels per unit. The bake lights the rest pose with the recipe's point lights and key light (key colour times `keyIntensity`). It runs when the scene builds and again only when a part, its matrix, a light, a material or the bake fields change: camera edits, effects edits, motion seeks and renders reuse the result. A bake that passes its time limit fails the edit and leaves the scene as it was. The limit is 90 s when the bake runs on a worker (the MCP preview does this, so it blocks nothing) and 20 s on the page's own thread when there is no Worker. The preview's call deadline grows by the 90 s for a `scene_create` whose recipe has a `bake` and for a `scene_apply` with `set-bake`; other calls keep 30 s. The same recipe gives the same lightmap bytes. `scene_inspect` returns `bake`: the resolved fields, `used` (the samples, bounces and texelsPerUnit that were traced), `reduced`, `texels`, `rayWork`, `seconds` (of the run that made the lightmaps), `bakedParts`, `skipped` (name and reason) and `reused`.+Ranges are 1 to 256 samples, 1 to 3 bounces and 1 to 12 texels per unit. The bake lights the rest pose with the recipe's point lights and key light (key colour times `keyIntensity`). It runs when the scene builds and again only when a part, its matrix, a light, a material or the bake fields change: camera edits, effects edits, motion seeks and renders reuse the result. A bake that passes its time limit fails the edit and leaves the scene as it was. The limit is 90 s when the bake runs on a worker (the MCP preview does this, so it blocks nothing) and 20 s on the page's own thread when there is no Worker. The preview's call deadline grows by the 90 s for a `scene_create` whose recipe has a `bake` and for a `scene_apply` with `set-bake`; other calls keep 30 s. A `scene_apply` also gets it whenever the active scene already has a bake, because an ordinary edit such as moving a part rebakes. The same recipe gives the same lightmap bytes. `scene_inspect` returns `bake`: the resolved fields, `used` (the samples, bounces and texelsPerUnit that were traced), `reduced`, `texels`, `rayWork`, `seconds` (of the run that made the lightmaps), `bakedParts`, `skipped` (name and reason) and `reused`.### Work budgetdiff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjsindex 74ea3940..0701dbba 100644--- a/scripts/agent-runtime.mjs+++ b/scripts/agent-runtime.mjs@@ -33,8 +33,14 @@ const EVALUATION_TIMEOUT_MS = 30000;/** A bake runs on a worker for up to its own limit, so a call that bakes gets that much more than a plain call. */const BAKE_EVALUATION_TIMEOUT_MS = EVALUATION_TIMEOUT_MS + BAKE_WORKER_MAXIMUM_SECONDS * 1000;-function startsBake(operations) {- return Array.isArray(operations) && operations.some(operation => operation?.type === 'set-bake' && operation.bake !== null && operation.bake !== undefined);+/** The last set-bake among the operations decides: a bake object adds one, null clears it. Undefined when there is none. */+function bakeAfterOperations(operations) {+ const setBakes = Array.isArray(operations) ? operations.filter(operation => operation?.type === 'set-bake') : [];+ if (setBakes.length === 0) {+ return undefined;+ }+ const lastBake = setBakes[setBakes.length - 1].bake;+ return lastBake !== null && lastBake !== undefined;}// Decoded audio and video lengths of a finished file may differ by the encoder delay; more than this fails the job.const SYNC_TOLERANCE_SECONDS = 0.05;@@ -79,6 +85,8 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browlet reviewPage;let cancelReview;let activeSessionId = null;+ // Whether the active scene has a bake, so an edit that rebakes gets the longer deadline.+ let activeSceneHasBake = false;let queuedCalls = 0;let queue = Promise.resolve();let closing;@@ -513,6 +521,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browconst sessionId = randomUUID();const result = await evaluate('create', { recipe, sessionId, memoryBudgetMiB: memory, backend }, page, recipe?.bake ? BAKE_EVALUATION_TIMEOUT_MS : EVALUATION_TIMEOUT_MS);activeSessionId = sessionId;+ activeSceneHasBake = Boolean(recipe?.bake);return result;});},@@ -555,7 +564,11 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browconst { branch, ...rest } = input;return invoke('branchApply', { ...rest, name: validateBranchName(branch) });}- return invoke('apply', input, startsBake(input.operations) ? BAKE_EVALUATION_TIMEOUT_MS : EVALUATION_TIMEOUT_MS);+ const bakeAfter = bakeAfterOperations(input.operations);+ const rebakes = activeSceneHasBake || bakeAfter === true;+ const applied = await invoke('apply', input, rebakes ? BAKE_EVALUATION_TIMEOUT_MS : EVALUATION_TIMEOUT_MS);+ activeSceneHasBake = bakeAfter ?? activeSceneHasBake;+ return applied;});},look(input) {diff --git a/tests/agent-runtime.test.js b/tests/agent-runtime.test.jsindex ce91654b..3d59815a 100644--- a/tests/agent-runtime.test.js+++ b/tests/agent-runtime.test.js@@ -32,8 +32,9 @@ test('runtime reports real scene capabilities without launching a browser and cl});async function reviewRuntimeHarness({ failShot, blockShot = false, oversize = false, deadlineMs,- failDispose = false, heapSize = 1024, stallPrivatePage = false, resumeDelayMs = 0 } = {}) {+ failDispose = false, heapSize = 1024, stallPrivatePage = false, resumeDelayMs = 0, sceneBake } = {}) {const events = [];+ const deadlines = [];let privatePage;let sourceRevision = 0;let resolveShotStarted;@@ -42,7 +43,7 @@ async function reviewRuntimeHarness({ failShot, blockShot = false, oversize = faconst acquisitionStarted = new Promise(resolve => { resolveAcquisitionStarted = resolve; });let resolvePrivatePage;const acquiredPage = new Promise(resolve => { resolvePrivatePage = () => resolve(privatePage = new FakePage(true)); });- const recipe = { format: '67js.scene', schemaVersion: 1, geometries: [], materials: [], nodes: [] };+ const recipe = { format: '67js.scene', schemaVersion: 1, geometries: [], materials: [], nodes: [], ...(sceneBake ? { bake: sceneBake } : {}) };class FakePage {constructor(privateTarget) { this.privateTarget = privateTarget; this.closed = false; }route() {}@@ -102,12 +103,13 @@ async function reviewRuntimeHarness({ failShot, blockShot = false, oversize = facreateRequire: () => ({ resolve: () => { throw new Error('no peer in tests'); } }),AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMES, RENDER_PASS_DEPTH_RANGE, VIDEO_ENCODERS, MAXIMUM_VIDEO_SECONDS, MAXIMUM_JOB_SIDE, MAXIMUM_JOB_PIXELS, MAXIMUM_MEMORY_MIB, BAKE_WORKER_MAXIMUM_SECONDS, PATH_TRACER_CAPABILITIES,audioInputArguments, createAudioLibrary, measureStreamSeconds, filmTimelineDuration, filmTimelineShotStarts, sampleFilmTimeline, normalizeReview, REVIEW_LIMITS, isPreviewHost, BRANCH_LIMITS, validateBranchName,- chromium: { launch: async () => browser }, URL, Buffer, process, performance, setTimeout, clearTimeout,+ chromium: { launch: async () => browser }, URL, Buffer, process, performance, clearTimeout,+ setTimeout: (callback, delay) => { deadlines.push(delay); return setTimeout(callback, delay); },});new Script(`${source}\nglobalThis.runtime = createAgentRuntime({ backend: 'cpu' });`).runInContext(context);const runtime = context.runtime;const created = await runtime.create({ recipe });- return { runtime, sessionId: created.sessionId, events, shotStarted, acquisitionStarted, resolvePrivatePage };+ return { runtime, sessionId: created.sessionId, deadlines, events, shotStarted, acquisitionStarted, resolvePrivatePage };}function boundedResult(promise) {@@ -117,6 +119,23 @@ function boundedResult(promise) {]).finally(() => clearTimeout(timer));}+test('an edit of a scene with a bake, or one that adds a bake, gets the bake deadline; a plain edit keeps 30 s', async () => {+ const plain = await reviewRuntimeHarness();+ await plain.runtime.apply({ sessionId: plain.sessionId, operations: [{ type: 'set-camera', camera: {} }] });+ await plain.runtime.apply({ sessionId: plain.sessionId, operations: [{ type: 'set-bake', bake: { preset: 'draft' } }] });+ await plain.runtime.apply({ sessionId: plain.sessionId, operations: [{ type: 'set-camera', camera: {} }] });+ await plain.runtime.apply({ sessionId: plain.sessionId, operations: [{ type: 'set-bake', bake: null }] });+ await plain.runtime.apply({ sessionId: plain.sessionId, operations: [{ type: 'set-camera', camera: {} }] });+ const applyDeadlines = plain.deadlines.slice(-5);+ assert.deepEqual(applyDeadlines, [30000, 120000, 120000, 120000, 30000]);+ await plain.runtime.close();+ const baked = await reviewRuntimeHarness({ sceneBake: { preset: 'draft' } });+ assert.equal(baked.deadlines.at(-1), 120000, 'creating a scene with a bake gets the bake deadline');+ await baked.runtime.apply({ sessionId: baked.sessionId, operations: [{ type: 'set-node', id: 'a' }] });+ assert.equal(baked.deadlines.at(-1), 120000, 'moving a part in a baked scene gets the bake deadline');+ await baked.runtime.close();+});+test('review deadline rejects stalled page acquisition and disposes a late page', async () => {const { runtime, sessionId, events, acquisitionStarted, resolvePrivatePage } = await reviewRuntimeHarness({stallPrivatePage: true, deadlineMs: 40,