3D engine that AI agents drive
Cap the combined asset bytes one MCP call sends to the preview page (#498)
Nguyen Xuan Anb7b6b2a076af
diff --git a/changes/498.md b/changes/498.mdnew file mode 100644index 00000000..8fdd3e2a--- /dev/null+++ b/changes/498.md@@ -0,0 +1 @@+- Cap the combined asset bytes one MCP call sends to the preview page at 64 MiB (issue #498). Several assets that were each under the per-file cap could together crash the preview page; the call now fails with an error that names `maxCombinedBytes` and the total.diff --git a/docs/mcp.md b/docs/mcp.mdindex 1f884674..1ca836c1 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -59,7 +59,7 @@ Using Node directly also avoids shell-specific npm launcher behavior in MCP clie`--memory` accepts an integer from 32 to 384 MiB and defaults to 192. A 1920x1080 frame needs about 175 MiB of render targets (measured, 4 samples), so the default fits an empty room at that size and 256 leaves room for textures; raise it for textured scenes.`--assets` selects an absolute directory containing self-contained GLB files.-The asset catalog reads regular files directly inside that directory, with limits of 64 files and 64 MiB per file. Decoding takes 1,000,000 vertices, 4,096 parts and 1,024 joints at most; `scene_capabilities` reports every number under `assets`. They are lower than the `loadGltf` defaults (256 MiB, 8,000,000 vertices) because the server hands the whole file to the preview page as one base64 string, which crashed the page from 80 MiB in a local Chrome test. A file over a limit fails with an error that names the limit and the value it hit; a larger scene loads through `loadGltf` in code (see [gltf.md](gltf.md)). All limits live in `src/agent-asset-limits.js`.+The asset catalog reads regular files directly inside that directory, with limits of 64 files and 64 MiB per file. One call that places several assets, motion sources and sequence frames sends them to the preview page in one message, so their total is capped at 64 MiB too (`maxCombinedBytes`); a call over it fails with an error that names the limit and the total, and the agent places the rest in later calls. Decoding takes 1,000,000 vertices, 4,096 parts and 1,024 joints at most; `scene_capabilities` reports every number under `assets`. They are lower than the `loadGltf` defaults (256 MiB, 8,000,000 vertices) because the server hands the whole file to the preview page as one base64 string, which crashed the page from 80 MiB in a local Chrome test. A file over a limit fails with an error that names the limit and the value it hit; a larger scene loads through `loadGltf` in code (see [gltf.md](gltf.md)). All limits live in `src/agent-asset-limits.js`.Asset IDs bind file contents through SHA-256. Changed files require a runtime restart before they receive refreshed IDs.Catalog imports retain supported textures, skins, morphs and animation clips.The catalog takes self-contained GLB only. Poly Haven and similar sites ship a glTF folder (`.gltf`, `.bin` and textures): bundle it first with one command, then put the result in the catalog directory: `npx @gltf-transform/cli copy tree_01_1k.gltf tree_01.glb`. A recipe places a catalog asset with an `asset` node field (see [the scene guide](agent-scene.md#placed-catalog-assets)).diff --git a/scripts/agent-runtime.mjs b/scripts/agent-runtime.mjsindex a48c9a95..5a470506 100644--- a/scripts/agent-runtime.mjs+++ b/scripts/agent-runtime.mjs@@ -15,7 +15,7 @@ import { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMESimport { PATH_TRACER_CAPABILITIES } from '../src/path-tracer-capabilities.js';import { chooseDenoiser, finishWithOidn } from './agent-path-trace-denoise.mjs';import { createAgentAssetCatalog } from './agent-asset-catalog.mjs';-import { AGENT_ASSET_LIMITS } from '../src/agent-asset-limits.js';+import { AGENT_ASSET_LIMITS, assertCombinedAssetBytes } from '../src/agent-asset-limits.js';import { recipeAssetNeeds } from '../src/agent-scene-assets.js';import { sequenceFrameIds } from '../src/agent-texture-sequence.js';import { TEXTURE_SEQUENCE_CAPABILITIES } from '../src/texture-sequence-limits.js';@@ -331,6 +331,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', brow}async function evaluate(method, input, target = page, timeoutMs = EVALUATION_TIMEOUT_MS) {+ assertCombinedAssetBytes(input);let timer;let timedOut = false;try {@@ -588,7 +589,7 @@ export function createAgentRuntime({ kernelWorkers = true, backend = 'gpu', browavailableIn: packageMetadata.version,assets: {configured: Boolean(assetRoot), formats: ['self-contained GLB', 'PNG, JPEG or WebP image (map sequence frame)'],- maximumBytes: AGENT_ASSET_LIMITS.maxBytes, maximumAssets: AGENT_ASSET_LIMITS.maxAssets,+ maximumBytes: AGENT_ASSET_LIMITS.maxBytes, maximumAssets: AGENT_ASSET_LIMITS.maxAssets, maximumCombinedBytes: AGENT_ASSET_LIMITS.maxCombinedBytes,maximumVertices: AGENT_ASSET_LIMITS.maxVertices, maximumParts: AGENT_ASSET_LIMITS.maxParts, maximumTexturePixels: AGENT_ASSET_LIMITS.maxTexturePixels,maximumJoints: AGENT_ASSET_LIMITS.maxJoints, maximumClips: AGENT_ASSET_LIMITS.maxClips,commands: ['set-root', 'set-asset-node', 'set-motion', 'set-animation', 'set-material', 'set-camera', 'set-environment', 'set-effects', 'set-shadow'],diff --git a/src/agent-asset-limits.js b/src/agent-asset-limits.jsindex efd4bb33..8dcdf105 100644--- a/src/agent-asset-limits.js+++ b/src/agent-asset-limits.js@@ -9,6 +9,9 @@export const AGENT_ASSET_LIMITS = Object.freeze({maxBytes: 64 * 1024 * 1024,maxAssets: 64,+ // The same ceiling for the sum of every asset one call hands to the preview page in one browser message (placed assets,+ // motion sources and sequence frames together). Files that are each under maxBytes can still cross the crash size together.+ maxCombinedBytes: 64 * 1024 * 1024,// About 32 bytes a vertex fills a 64 MiB file with 2,000,000; half of that keeps the decoded copy inside the preview memory budget.maxVertices: 1_000_000,maxParts: 4096,@@ -36,6 +39,35 @@ export function assetBytesLimitMessage(size) {return `Asset is ${size} bytes, over the catalog maxBytes ${limit} (${describeAssetBytes(limit)}). Shrink it (meshopt compression, smaller textures) or split it into parts; a larger scene loads through loadGltf in code.`;}+/** Message for one call whose assets together pass the combined limit; it names the limit, the total and the way out. */+export function combinedAssetBytesLimitMessage(total) {+ const limit = AGENT_ASSET_LIMITS.maxCombinedBytes;+ return `This call sends ${total} bytes of assets to the preview page at once, over the catalog maxCombinedBytes ${limit} (${describeAssetBytes(limit)}). Place fewer or smaller assets in one call and add the rest in later calls, or shrink them (meshopt compression, smaller textures).`;+}++function decodedBase64Bytes(text) {+ const padding = text.endsWith('==') ? 2 : text.endsWith('=') ? 1 : 0;+ return Math.floor(text.length * 3 / 4) - padding;+}++/** Decoded bytes of every base64 asset field in the input of one preview call. */+export function combinedAssetBytes(input) {+ let total = 0;+ for (const field of ['assetData', 'sequenceData', 'sources']) {+ for (const text of Object.values(input?.[field] ?? {})) {+ if (typeof text === 'string') total += decodedBase64Bytes(text);+ }+ }+ if (typeof input?.data === 'string') total += decodedBase64Bytes(input.data);+ return total;+}++/** Throws a RangeError when the assets in one preview call pass the combined limit. */+export function assertCombinedAssetBytes(input) {+ const total = combinedAssetBytes(input);+ if (total > AGENT_ASSET_LIMITS.maxCombinedBytes) throw new RangeError(combinedAssetBytesLimitMessage(total));+}+/*** parseGltf tells a code caller to "pass { maxVertices: n }", which an MCP agent cannot do. Swap that advice for the* catalog limits and the way out.diff --git a/tests/agent-asset-catalog.test.js b/tests/agent-asset-catalog.test.jsindex 04b9bba0..cf327004 100644--- a/tests/agent-asset-catalog.test.js+++ b/tests/agent-asset-catalog.test.js@@ -97,7 +97,7 @@ test('catalog rejects linked assets and requires an explicitly absolute directortest('the catalog limits are the documented values and never exceed the loadGltf defaults', () => {assert.deepEqual({ ...AGENT_ASSET_LIMITS }, {- maxBytes: 64 * 1024 * 1024, maxAssets: 64, maxVertices: 1_000_000, maxParts: 4096,+ maxBytes: 64 * 1024 * 1024, maxAssets: 64, maxCombinedBytes: 64 * 1024 * 1024, maxVertices: 1_000_000, maxParts: 4096,maxTexturePixels: 4_194_304, maxJoints: 1024, maxClips: 256,});const defaults = GLTF_CAPABILITIES.limits;diff --git a/tests/agent-combined-asset-limit.test.js b/tests/agent-combined-asset-limit.test.jsnew file mode 100644index 00000000..ec10ab5d--- /dev/null+++ b/tests/agent-combined-asset-limit.test.js@@ -0,0 +1,38 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { AGENT_ASSET_LIMITS, assertCombinedAssetBytes, combinedAssetBytes } from '../src/agent-asset-limits.js';++const MEBIBYTE = 1024 * 1024;++function base64OfSize(byteCount) {+ return Buffer.alloc(byteCount).toString('base64');+}++test('combinedAssetBytes counts decoded bytes of every asset field in one call', () => {+ const input = {+ assetData: { a: base64OfSize(10), b: base64OfSize(11) },+ sequenceData: { c: base64OfSize(12) },+ sources: { d: base64OfSize(13) },+ data: base64OfSize(14),+ };+ assert.equal(combinedAssetBytes(input), 60);+ assert.equal(combinedAssetBytes({}), 0);+});++test('a call whose assets are each under maxBytes but together over maxCombinedBytes fails naming limit and total', () => {+ const each = 40 * MEBIBYTE;+ assert.ok(each < AGENT_ASSET_LIMITS.maxBytes);+ const input = { assetData: { first: base64OfSize(each), second: base64OfSize(each) } };+ assert.throws(+ () => assertCombinedAssetBytes(input),+ error => error instanceof RangeError+ && error.message.includes('maxCombinedBytes')+ && error.message.includes(String(AGENT_ASSET_LIMITS.maxCombinedBytes))+ && error.message.includes(String(2 * each)),+ );+});++test('a call at exactly maxCombinedBytes passes', () => {+ const half = AGENT_ASSET_LIMITS.maxCombinedBytes / 2;+ assert.doesNotThrow(() => assertCombinedAssetBytes({ assetData: { first: base64OfSize(half) }, sources: { second: base64OfSize(half) } }));+});diff --git a/tests/agent-runtime.test.js b/tests/agent-runtime.test.jsindex 5932a477..2c10ca53 100644--- a/tests/agent-runtime.test.js+++ b/tests/agent-runtime.test.js@@ -15,7 +15,7 @@ import { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, RENDER_PASS_NAMESimport { recipeAssetNeeds } from '../src/agent-scene-assets.js';import { sequenceFrameIds } from '../src/agent-texture-sequence.js';import { TEXTURE_SEQUENCE_CAPABILITIES } from '../src/texture-sequence-limits.js';-import { AGENT_ASSET_LIMITS } from '../src/agent-asset-limits.js';+import { AGENT_ASSET_LIMITS, assertCombinedAssetBytes } from '../src/agent-asset-limits.js';import { PATH_TRACER_CAPABILITIES } from '../src/path-tracer-capabilities.js';import { audioInputArguments, createAudioLibrary, measureStreamSeconds } from '../scripts/job-audio.mjs';import { filmTimelineDuration, filmTimelineShotStarts, sampleFilmTimeline } from '../src/film-timeline.js';@@ -111,7 +111,7 @@ async function reviewRuntimeHarness({ failShot, blockShot = false, oversize = fareadFile: async () => '', packageMetadata: { version: 'test' },createRequire: () => ({ resolve: () => { throw new Error('no peer in tests'); } }),VEGETATION_CAPABILITY_NOTE, CROWD_CAPABILITY_NOTE, USER_PASS_CAPABILITIES, MESH_BLEND_CAPABILITIES, MATERIAL_SNIPPET_CAPABILITIES, 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,- recipeAssetNeeds, sequenceFrameIds, TEXTURE_SEQUENCE_CAPABILITIES, AGENT_ASSET_LIMITS,+ recipeAssetNeeds, sequenceFrameIds, TEXTURE_SEQUENCE_CAPABILITIES, AGENT_ASSET_LIMITS, assertCombinedAssetBytes,audioInputArguments, createAudioLibrary, measureStreamSeconds, filmTimelineDuration, filmTimelineShotStarts, sampleFilmTimeline, normalizeReview, REVIEW_LIMITS, isPreviewHost, BRANCH_LIMITS, validateBranchName,chromium: { launch: async () => browser }, URL, Buffer, process, performance, clearTimeout,setTimeout: (callback, delay) => { deadlines.push(delay); return setTimeout(callback, delay); },