Skip to content

3D engine that AI agents drive

Name the nodes and the fix when removing a geometry or material in use (#2110)

Nguyen Xuan An0df5bea163df
diff --git a/changes/2110.md b/changes/2110.md
new file mode 100644
index 00000000..3eda9d9c
--- /dev/null
+++ b/changes/2110.md
@@ -0,0 +1 @@
+- `remove-geometry` and `remove-material` on a resource that nodes still use now name the nodes (first 8, then a count) and the fix, and no field error prints an empty "expected one of ." list (#2110)
diff --git a/changes/2110.size.md b/changes/2110.size.md
new file mode 100644
index 00000000..a3e5aa63
--- /dev/null
+++ b/changes/2110.size.md
@@ -0,0 +1,3 @@
+- scene:agent-scene: the in-use removal error names the nodes and the fix, about 215 bytes gzip, so an agent no longer gets an empty "expected one of ." with no way forward
+- scene:agent-scene+lazy: the same in-use removal error, about 205 bytes gzip
+- goal:agent-scene: an agent-facing error that names the nodes and the fix replaces a message with an empty list; the scene is already over its goal and this adds about 215 bytes gzip
diff --git a/docs/mcp.md b/docs/mcp.md
index 68968f1b..f6103b0b 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -322,7 +322,7 @@ Build from an empty recipe by adding resources and nodes in one revisioned batch
]
```
-Use `remove-node`, `remove-geometry` and `remove-material` with an `id` to remove entries.
+Use `remove-node`, `remove-geometry` and `remove-material` with an `id` to remove entries. A geometry or material that nodes still use cannot be removed: the error names those nodes, so `set-node` them to another one or `remove-node` them first.
References must resolve after the complete batch. Removing a parent leaves its children unchanged unless `recursive: true` removes its subtree.
Reparent or remove those children before the parent removal command to avoid rejection.
The [scene contract](agent-scene.md) defines the geometry fields, transforms, materials, and atomic validation rules.
diff --git a/src/agent-scene.js b/src/agent-scene.js
index 628c864a..a2306bce 100644
--- a/src/agent-scene.js
+++ b/src/agent-scene.js
@@ -78,6 +78,17 @@ const REMOVAL_FIELDS = Object.freeze({
'remove-geometry': 'geometries',
'remove-material': 'materials',
});
+// Rejects a batch that removes a geometry or material and leaves nodes pointing at it, naming them and the fix.
+function assertRemovedResourcesUnused(candidate, removals) {
+ for (const { type, id } of removals) {
+ const kind = type.slice(7);
+ const users = candidate.nodes.filter(node => node[kind] === id).map(node => node.id);
+ if (users.length > 0 && !candidate[REMOVAL_FIELDS[type]].some(record => record.id === id)) {
+ const more = users.length > 8 ? ` and ${users.length - 8} more` : '';
+ throw invalidField('id', id, `${kind} ${id} is used by nodes ${users.slice(0, 8).join(', ')}${more}; set-node them to another ${kind} or remove-node them first`);
+ }
+ }
+}
function knownKeys(value, allowed, label) {
@@ -2154,6 +2165,7 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {
const candidate = (onlyFieldEdits && fieldEditCandidate(state.recipe, operations)) || authoredRecipe(state.recipe);
const removedNodes = new Set();
const removedMaterials = new Set();
+ const removals = [];
for (const operation of operations) {
const section = Object.hasOwn(NULLABLE_SECTION_OPERATIONS, operation.type) ? NULLABLE_SECTION_OPERATIONS[operation.type] : null;
if (section) {
@@ -2265,6 +2277,7 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {
if (index < 0) {
throw invalidField('id', operation.id, `the ID of an existing ${removal} entry; scene_inspect lists them`);
}
+ removals.push(operation);
candidate[removal].splice(index, 1);
if (operation.type === 'remove-material') {
removedMaterials.add(operation.id);
@@ -2299,6 +2312,7 @@ export function createAgentScene(input, inputLimits = {}, options = {}) {
}
}
}
+ assertRemovedResourcesUnused(candidate, removals);
// Plain field edits put only values the operation copy already checked into the candidate, so the recipe is not copied and checked a second time.
try {
return commit(stage(candidate, null, removedNodes, removedMaterials, onlyFieldEdits, onlyFieldEdits ? poseEditNodeIds(operations) : null), true);
diff --git a/src/field-error.js b/src/field-error.js
index 42f49cff..7d6ca6f4 100644
--- a/src/field-error.js
+++ b/src/field-error.js
@@ -22,6 +22,9 @@ export function describeReceived(value) {
// Builds the validation error an agent-facing check throws: the field path, the value received and what is accepted,
// with an example or a range. withFieldPrefix turns the path into a full recipe path for entries of a list.
export function invalidField(path, value, expected, ErrorType = RangeError) {
+ if (/^one of\s*$/.test(expected)) {
+ expected = 'an ID that exists, but none is defined yet; add one first';
+ }
const error = new ErrorType(`${path}: got ${describeReceived(value)}; expected ${expected}.`);
error.fieldPath = path;
return error;
diff --git a/tests/agent-scene.test.js b/tests/agent-scene.test.js
index 7fe3d2a4..20824798 100644
--- a/tests/agent-scene.test.js
+++ b/tests/agent-scene.test.js
@@ -711,3 +711,28 @@ test('kept revisions never block an edit that fits the geometry budget without t
assert.equal(session.inspect().geometryBytes, 720);
assert.throws(() => session.restoreRevision(0, { expectedRevision: 2 }), /kept revisions: 1/);
});
+
+test('removing a geometry or material that nodes still use names the nodes and the fix', () => {
+ const session = createAgentScene(recipe());
+ assert.throws(() => session.apply([{ type: 'remove-geometry', id: 'box' }], { expectedRevision: 0 }),
+ /id: got "box"; expected geometry box is used by nodes child, other; set-node them to another geometry or remove-node them first./);
+ assert.throws(() => session.apply([{ type: 'remove-material', id: 'finish' }], { expectedRevision: 0 }),
+ /id: got "finish"; expected material finish is used by nodes child, other; set-node them to another material or remove-node them first./);
+ session.apply([{ type: 'add-geometry', geometry: { id: 'spare', type: 'box', bevel: 0 } }, { type: 'remove-geometry', id: 'spare' }], { expectedRevision: 0 });
+});
+
+test('the in-use removal error lists at most eight nodes and counts the rest', () => {
+ const crowded = recipe();
+ for (let index = 0; index < 11; index += 1) {
+ crowded.nodes.push({ id: `extra${index}`, geometry: 'box', material: 'finish' });
+ }
+ const session = createAgentScene(crowded);
+ assert.throws(() => session.apply([{ type: 'remove-geometry', id: 'box' }], { expectedRevision: 0 }),
+ /nodes child, other, extra0, extra1, extra2, extra3, extra4, extra5 and 5 more;/);
+});
+
+test('field errors never print an empty list of choices', () => {
+ const session = createAgentScene({ format: 'crucible.scene', schemaVersion: 1, geometries: [], materials: [], nodes: [] });
+ assert.throws(() => session.apply([{ type: 'add-node', node: { id: 'a', geometry: 'box', material: 'm' } }], { expectedRevision: 0 }),
+ error => !/expected one of \./.test(error.message) && /none is defined yet/.test(error.message));
+});