Skip to content

3D engine that AI agents drive

Guard the MCP log summary, native memory allowance and GLB chunk length (#2571)

Nguyen Xuan Anb9a2a1875b3b
diff --git a/changes/2571.md b/changes/2571.md
new file mode 100644
index 00000000..f47b966f
--- /dev/null
+++ b/changes/2571.md
@@ -0,0 +1 @@
+- Job script guards: the MCP log summary skips lines that are not call records, the native memory allowance refuses a NaN or negative --memory budget, and a GLB whose JSON chunk runs past the file gets a clear error (#2571, #2572, #2573)
diff --git a/scripts/agent-memory-guard.mjs b/scripts/agent-memory-guard.mjs
index ebabd0b7..eb99aac6 100644
--- a/scripts/agent-memory-guard.mjs
+++ b/scripts/agent-memory-guard.mjs
@@ -9,14 +9,18 @@ const NODE_RSS_MARGIN_MIB = 128;
// The browser heap is the scene's JavaScript objects, a share of the budget; two thirds of 384 MiB is the old 256 MiB.
const BROWSER_HEAP_SHARE = 2 / 3;
+function requireMemoryBudget(memoryMiB) {
+ if (!Number.isFinite(memoryMiB) || memoryMiB < 0) {
+ throw new RangeError(`The memory guard needs a memory budget of zero or more MiB, got ${memoryMiB}.`);
+ }
+}
+
/**
* The two limits of a job that runs with `--memory memoryMiB`. They follow the budget instead of fixed constants (#1776, #1696),
* so raising --memory (up to what the machine holds, see maximumMemoryMiB) raises the guard with it, and never below the floors above.
*/
export function memoryGuardLimits(memoryMiB = 0) {
- if (!Number.isFinite(memoryMiB) || memoryMiB < 0) {
- throw new RangeError(`memoryGuardLimits needs a memory budget of zero or more MiB, got ${memoryMiB}.`);
- }
+ requireMemoryBudget(memoryMiB);
return {
nodeBytes: Math.max(NODE_RSS_LIMIT_BYTES, (memoryMiB + NODE_RSS_MARGIN_MIB) * 1024 ** 2),
browserHeapBytes: Math.max(BROWSER_HEAP_LIMIT_BYTES, Math.ceil(memoryMiB * BROWSER_HEAP_SHARE) * 1024 ** 2),
@@ -72,6 +76,7 @@ export function nativeNodeAllowanceBytes({ width, height, memoryMiB = 0 }) {
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1) {
throw new Error(`nativeNodeAllowanceBytes needs a positive integer width and height, got ${width} x ${height}.`);
}
+ requireMemoryBudget(memoryMiB);
const sceneBytes = memoryMiB * NATIVE_SCENE_MIB_PER_BUDGET_MIB * 1024 ** 2;
const allowance = NATIVE_FIXED_ALLOWANCE_BYTES + width * height * NATIVE_BYTES_PER_PIXEL + sceneBytes;
return Math.min(allowance, NATIVE_ALLOWANCE_CAP_BYTES + sceneBytes);
diff --git a/scripts/agent-query-asset.mjs b/scripts/agent-query-asset.mjs
index 4df5e031..9caac468 100644
--- a/scripts/agent-query-asset.mjs
+++ b/scripts/agent-query-asset.mjs
@@ -22,6 +22,9 @@ export function geometryOnlyGlb(bytes) {
throw new Error('Query-only assets must be GLB files with a JSON chunk first.');
}
const jsonLength = view.getUint32(12, true);
+ if (20 + jsonLength > bytes.byteLength) {
+ throw new Error(`The GLB JSON chunk is ${jsonLength} bytes, longer than the file. Pass the complete GLB.`);
+ }
const document = JSON.parse(Buffer.from(bytes.subarray(20, 20 + jsonLength)).toString('utf8'));
delete document.images;
delete document.textures;
diff --git a/scripts/summarize-mcp-log.mjs b/scripts/summarize-mcp-log.mjs
index 415511d1..e60be68b 100644
--- a/scripts/summarize-mcp-log.mjs
+++ b/scripts/summarize-mcp-log.mjs
@@ -2,6 +2,10 @@
import { readFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
+function isCallRecord(value) {
+ return typeof value?.tool === 'string' && Number.isFinite(value.durationMs);
+}
+
export function readCalls(logPaths) {
const calls = [];
for (const logPath of logPaths) {
@@ -9,10 +13,16 @@ export function readCalls(logPaths) {
if (line.trim() === '') {
continue;
}
+ let call;
try {
- calls.push(JSON.parse(line));
+ call = JSON.parse(line);
} catch {
- console.error(`Skipped a line in ${logPath} that is not JSON.`);
+ call = undefined;
+ }
+ if (isCallRecord(call)) {
+ calls.push(call);
+ } else {
+ console.error(`Skipped a line in ${logPath} that is not a call record.`);
}
}
}
diff --git a/tests/agent-memory-guard.test.js b/tests/agent-memory-guard.test.js
index ab35250b..e3212bbf 100644
--- a/tests/agent-memory-guard.test.js
+++ b/tests/agent-memory-guard.test.js
@@ -236,3 +236,10 @@ test('the runtime hands its --memory to the sampler, the guard and the native al
assert.equal((source.match(/memoryGuardLimits\(memory\)\.browserHeapBytes/g) || []).length, 2);
assert.match(source, /memory > maximumMemory/);
});
+
+test('a native allowance refuses a memory budget that is not a finite number of zero or more MiB', () => {
+ for (const memoryMiB of [Number.NaN, Number.POSITIVE_INFINITY, -1, -9999]) {
+ assert.throws(() => nativeNodeAllowanceBytes({ width: 64, height: 64, memoryMiB }), /zero or more MiB/, `memoryMiB ${memoryMiB}`);
+ }
+ assert.equal(nativeNodeAllowanceBytes({ width: 64, height: 64, memoryMiB: 0 }), 640 * MEBIBYTE + 64 * 64 * 1024);
+});
diff --git a/tests/agent-query-asset.test.js b/tests/agent-query-asset.test.js
index ee9ea1aa..fd5eb8d7 100644
--- a/tests/agent-query-asset.test.js
+++ b/tests/agent-query-asset.test.js
@@ -26,3 +26,9 @@ test('the query-only cache asks for the file once, walks it and replaces it for
test('a file that is not a GLB with a JSON chunk first is refused', () => {
assert.throws(() => geometryOnlyGlb(Buffer.alloc(64)), /GLB/);
});
+
+test('a GLB whose JSON chunk is longer than the file says so', () => {
+ const whole = Buffer.from(carAsset());
+ const jsonLength = whole.readUInt32LE(12);
+ assert.throws(() => geometryOnlyGlb(whole.subarray(0, 20 + jsonLength - 8)), /JSON chunk.*longer than the file/);
+});
diff --git a/tests/mcp-call-log.test.js b/tests/mcp-call-log.test.js
index b5da2280..c1eaf323 100644
--- a/tests/mcp-call-log.test.js
+++ b/tests/mcp-call-log.test.js
@@ -97,3 +97,21 @@ test('the summary script reads several log files and prints a table', async cont
assert.equal(run.status, 0, run.stderr);
assert.match(run.stdout, /scene_look\s+2\s+1\s+50\.0%\s+6\.0/);
});
+
+test('the summary skips lines that parse as JSON but are not call records', async () => {
+ const folder = await mkdtemp(join(tmpdir(), 'crucible-call-log-'));
+ const logPath = join(folder, 'calls.jsonl');
+ const record = { tool: 'scene_look', ok: true, durationMs: 4 };
+ await writeFile(logPath, ['null', '5', '{"ok":true}', JSON.stringify({ tool: 'scene_look', ok: true }), JSON.stringify(record)].join('\n'));
+ const { readCalls } = await import('../scripts/summarize-mcp-log.mjs');
+ const originalError = console.error;
+ const notes = [];
+ console.error = message => notes.push(message);
+ try {
+ assert.deepEqual(readCalls([logPath]), [record]);
+ } finally {
+ console.error = originalError;
+ await rm(folder, { recursive: true, force: true });
+ }
+ assert.equal(notes.length, 4);
+});