Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): stop a nested attachment from measuring zero from itself (#4631)

[._.]/ Adam Eivya7c228283c2a
diff --git a/server/lib/threejsModelPhysicalAudit.js b/server/lib/threejsModelPhysicalAudit.js
index 7a33d33682..5aca378383 100644
--- a/server/lib/threejsModelPhysicalAudit.js
+++ b/server/lib/threejsModelPhysicalAudit.js
@@ -416,19 +416,26 @@ const unionBounds = (a, b) => {
return [0, 1, 2].map((axis) => [Math.min(a[axis][0], b[axis][0]), Math.max(a[axis][1], b[axis][1])]);
};
+const isInSubtree = (volume, partId) => volume.id === partId || volume.ancestorIds.includes(partId);
+
/**
- * World bounds for each part INCLUDING its descendants.
+ * World bounds for one part INCLUDING its descendants, optionally minus one
+ * nested subtree.
*
* An anchor is routinely a bare group whose surface is entirely in its children
* — "head" holding a skull and a jaw — so measuring only the part's own geometry
- * would report a hat as unmeasurable against the head it plainly sits on.
+ * would report a hat as unmeasurable against the head it plainly sits on. But an
+ * attachment is usually parented UNDER its anchor, and folding the attachment's
+ * own geometry into the anchor's bounds would make every nested attachment
+ * measure zero units away from itself: the check would pass a hat at hip height
+ * precisely because it is a child of the head. Hence the exclusion.
*/
-const buildSubtreeBounds = (volumes) => {
- const bounds = new Map();
+const subtreeBounds = (volumes, partId, excludePartId = null) => {
+ let bounds = null;
for (const volume of volumes) {
- for (const id of [volume.id, ...volume.ancestorIds]) {
- bounds.set(id, unionBounds(bounds.get(id), volume.worldBounds));
- }
+ if (!isInSubtree(volume, partId)) continue;
+ if (excludePartId && isInSubtree(volume, excludePartId)) continue;
+ bounds = unionBounds(bounds, volume.worldBounds);
}
return bounds;
};
@@ -444,7 +451,7 @@ const formatOffset = (value) => Number(value.toFixed(3));
* one pose.
*
* @returns {{findings: Array, unmeasured: Array<{partId: string, anchorPartId: string|null,
- * anchorSocket: string|null, reason: string}>}}
+ * anchorSocket: string|null, reason: string}>, measured: Set<string>}}
*/
function measureAttachmentAnchors({
attachments,
@@ -456,15 +463,17 @@ function measureAttachmentAnchors({
}) {
const findings = [];
const unmeasured = [];
- if (attachments.length === 0) return { findings, unmeasured };
+ // Which attachments this pose could actually put a number on, so a pose that
+ // could not see an anchor never overrides a pose that could.
+ const measured = new Set();
+ if (attachments.length === 0) return { findings, unmeasured, measured };
const measurable = volumes.filter((volume) => volume.visible && volume.opacity > 0);
- const subtreeBounds = buildSubtreeBounds(measurable);
const label = (partId) => `"${namesByPartId.get(partId) || partId}"`;
const { clipName, ...metadata } = context;
for (const attachment of attachments) {
- const attachmentBounds = subtreeBounds.get(attachment.partId);
+ const attachmentBounds = subtreeBounds(measurable, attachment.partId);
if (!attachmentBounds) {
unmeasured.push({
partId: attachment.partId,
@@ -485,7 +494,7 @@ function measureAttachmentAnchors({
anchorDescription = `socket "${attachment.anchorSocket}" on ${label(socket.parentPartId)}`;
}
} else if (attachment.anchorPartId) {
- anchorBounds = subtreeBounds.get(attachment.anchorPartId) || null;
+ anchorBounds = subtreeBounds(measurable, attachment.anchorPartId, attachment.partId);
anchorDescription = label(attachment.anchorPartId);
}
@@ -501,6 +510,7 @@ function measureAttachmentAnchors({
continue;
}
+ measured.add(attachment.partId);
const distance = aabbDistance(attachmentBounds, anchorBounds);
if (distance <= attachment.maxOffset) continue;
@@ -524,7 +534,7 @@ function measureAttachmentAnchors({
});
}
- return { findings, unmeasured };
+ return { findings, unmeasured, measured };
}
const collectAnimatedScaleSamples = (part, sequences) => sequences
@@ -597,7 +607,12 @@ export function evaluateThreejsPhysicalAudit(spec) {
socketsByName,
namesByPartId,
});
- const unmeasuredAttachments = staticAnchorReport.unmeasured;
+ // Accumulated across every pose, and resolved at the end: an attachment
+ // measured in ANY pose was checked, so a clip pose that happens to hide the
+ // anchor must not retroactively report it as unverified — and an attachment no
+ // pose could measure must never read as clean.
+ const measuredAnywhere = new Set(staticAnchorReport.measured);
+ const unmeasuredByPartId = new Map(staticAnchorReport.unmeasured.map((entry) => [entry.partId, entry]));
const staticAnchorFailures = new Set();
for (const finding of staticAnchorReport.findings) {
staticAnchorFailures.add(finding.partIds[0]);
@@ -759,6 +774,10 @@ export function evaluateThreejsPhysicalAudit(spec) {
namesByPartId,
context: { clipId: clip.id, clipName: clip.name || clip.id, timeSeconds },
});
+ for (const partId of report.measured) measuredAnywhere.add(partId);
+ for (const entry of report.unmeasured) {
+ if (!unmeasuredByPartId.has(entry.partId)) unmeasuredByPartId.set(entry.partId, entry);
+ }
for (const finding of report.findings) {
const partId = finding.partIds[0];
// The resting pose already reported this attachment; repeating it per
@@ -812,6 +831,9 @@ export function evaluateThreejsPhysicalAudit(spec) {
}
}
+ const unmeasuredAttachments = [...unmeasuredByPartId.values()]
+ .filter((entry) => !measuredAnywhere.has(entry.partId));
+
const countBy = (severity) => findings.filter((f) => f.severity === severity).length;
return {
diff --git a/server/lib/threejsModelPhysicalAudit.test.js b/server/lib/threejsModelPhysicalAudit.test.js
index 85488777ee..035b9f4441 100644
--- a/server/lib/threejsModelPhysicalAudit.test.js
+++ b/server/lib/threejsModelPhysicalAudit.test.js
@@ -801,6 +801,60 @@ describe('threejsModelPhysicalAudit attachment anchors', () => {
expect(res.findings.filter((f) => f.code === 'attachment-far-from-anchor')).toEqual([]);
});
+ // The natural authoring shape, and the one that defeats a naive subtree
+ // measurement: the hat is a CHILD of the head, so folding its own geometry into
+ // the head's bounds would measure it zero units from itself no matter where it
+ // sits. The anchor's bounds exclude the attachment's own subtree.
+ it('measures a nested attachment against its anchor rather than against itself', () => {
+ const nested = {
+ name: 'Figure',
+ materials: { skin: { type: 'standard', color: '#ffffff' } },
+ parts: [{
+ ...box('torso', 'Torso', [0, 1, 0], 1.2),
+ children: [{
+ ...box('head', 'Head', [0, 0.9, 0], 0.5),
+ // Authored on the head but positioned at hip height — the upstream defect.
+ children: [box('hat', 'Hat', [0, -1.5, 0], 0.55)],
+ }],
+ }],
+ sockets: [],
+ articulation: {
+ joints: [{ id: 'rootJoint', partId: 'torso', parentJointId: null, pivotSocket: null }],
+ attachmentPartIds: [],
+ attachments: [{ partId: 'hat', anchorPartId: 'head', anchorSocket: null, maxOffset: 0.25 }],
+ },
+ };
+ const [finding] = evaluateThreejsPhysicalAudit(nested).findings.filter((f) => f.code === 'attachment-far-from-anchor');
+ expect(finding).toBeDefined();
+ expect(finding.distance).toBeGreaterThan(0.25);
+
+ // …and the same hierarchy with the hat actually on the head stays clean.
+ nested.parts[0].children[0].children = [box('hat', 'Hat', [0, 0.4, 0], 0.55)];
+ expect(evaluateThreejsPhysicalAudit(nested).findings.filter((f) => f.code === 'attachment-far-from-anchor')).toEqual([]);
+ });
+
+ // A clip pose that hides the anchor did not disprove the resting-pose
+ // measurement, so it must not turn a checked attachment into an unchecked one.
+ it('keeps an attachment measured in the resting pose out of the unmeasured list', () => {
+ const spec = wornSpec([{ partId: 'hat', anchorPartId: 'head', anchorSocket: null, maxOffset: 0.25 }]);
+ spec.animation = {
+ clips: [{
+ id: 'vanish',
+ name: 'Vanish',
+ durationSeconds: 1,
+ sequences: [{
+ id: 'hideHead',
+ name: 'Hide head',
+ partId: 'head',
+ startSeconds: 0,
+ endSeconds: 1,
+ channels: { visible: { from: true, to: false } },
+ }],
+ }],
+ };
+ expect(evaluateThreejsPhysicalAudit(spec).unmeasuredAttachments).toEqual([]);
+ });
+
it('catches a clip that carries an attachment away from its anchor mid-clip', () => {
const spec = wornSpec([{ partId: 'hat', anchorPartId: 'head', anchorSocket: null, maxOffset: 0.25 }]);
spec.animation = {