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

feat: flag bilateral limbs mirrored by a yaw or a negative scale instead of a reflection (#5452)

[._.]/ Adam Eivyc652074f9caa
diff --git a/server/lib/README.md b/server/lib/README.md
index e54036b2de..a8bf93239c 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -233,7 +233,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `threejsModelEnvironment.js` | The image-based-lighting and render-profile half of the procedural sculpt contract, kept **dependency-free** so the client suite can import it to assert parity with `client/src/lib/threejsEnvironment.js` (`threejsModel.js` pulls in zod, which the client CI job does not install). `THREEJS_ENVIRONMENT_PRESETS` (`none`/`neutral`/`studio`) is the bounded preset list the spec schema builds its enum from; `resolveThreejsEnvironment(spec)` normalizes a spec's `{ preset, intensity }` block, reading an absent, partial, or unrecognized one as `DEFAULT_THREEJS_ENVIRONMENT` per field — a record stored before the block shipped has no key and was rendered with no environment, so `none` is the honest reading of it. `THREEJS_RENDER_PROFILE` is the output-colour-space / tone-mapping / exposure contract `buildThreejsFactorySource()` stamps onto every export. |
| `threejsModelFamilies.js` | Curated subject-family checklists for Three.js generation. `THREEJS_MODEL_FAMILY_OPTIONS` / `THREEJS_MODEL_FAMILY_IDS` drive the picker and route validation; `buildThreejsFamilyChecklist(id)` returns the prompt block spliced into generation (`''` for the default `general`, so an un-narrowed subject keeps the unchanged general-purpose prompt); `findMissingFamilyComponents(spec, id)` reports which required components the spec never mentions. |
| `threejsModelPenetration.js` | `evaluateThreejsPenetration(spec)` — cross-part penetration gate over an already-validated Three.js spec, the one check that compares two parts to each other. Rebuilds each part's world placement (the same `THREE.Euler` 'XYZ' composition the preview applies), derives an analytic solid per geometry type, and samples one part's interior against another's to flag a part entirely buried inside an unrelated one (`error`) or unrelated parts occupying substantially the same space (`warning`). Parts in the same subtree and declared `attachmentPartIds` are exempt — embedding is what those relationships are *for* — and a shallow or transparent-container overlap is reported as an undecided `note`, never as a defect. `buildThreejsPenetrationFeedback(penetration)` turns the errors and warnings (never the undecided notes) into default refinement feedback. |
-| `threejsModelPhysicalAudit.js` | `evaluateThreejsPhysicalAudit(spec)` — pure bounds and pose audit gate over an already-validated Three.js spec: inspects static resting poses and animated clip poses to detect floating parts (`floating-part`), swallowed geometry (`buried-geometry`), z-fighting coplanar surfaces (`coplanar-surface`), unprovenanced appearing geometry (`unprovenanced-transition`), non-uniform parent scale cascading into descendants (`nonuniform-parent-scale`), attachments declared with nothing to hang from (`unanchored-attachment`, warning), and attachments measured further from their declared anchor than `maxOffset` allows (`attachment-far-from-anchor`, error — the spec asserted the relationship itself), across the resting pose and the sampled clip poses so a clip that carries an attachment away from its anchor is caught too. An attachment whose anchor geometry could not be measured is listed in `unmeasuredAttachments` rather than counted as passing. `buildThreejsPhysicalAuditFeedback(physicalAudit)` turns actionable findings into default refinement feedback. |
+| `threejsModelPhysicalAudit.js` | `evaluateThreejsPhysicalAudit(spec)` — pure bounds and pose audit gate over an already-validated Three.js spec: inspects static resting poses and animated clip poses to detect floating parts (`floating-part`), swallowed geometry (`buried-geometry`), z-fighting coplanar surfaces (`coplanar-surface`), unprovenanced appearing geometry (`unprovenanced-transition`), non-uniform parent scale cascading into descendants (`nonuniform-parent-scale`), attachments declared with nothing to hang from (`unanchored-attachment`, warning), and attachments measured further from their declared anchor than `maxOffset` allows (`attachment-far-from-anchor`, error — the spec asserted the relationship itself), across the resting pose and the sampled clip poses so a clip that carries an attachment away from its anchor is caught too. Named left/right pairs are additionally audited for handedness in the resting pose, which no bounds check can see — a limb mirrored by a 180° yaw about the vertical axis rather than a lateral reflection (`bilateral-chirality`), by a negated scale component (`bilateral-mirror-scale`), or not mirrored at all so both halves sit on one side of the lateral plane (`bilateral-pair-same-side`), all warnings. An attachment whose anchor geometry could not be measured is listed in `unmeasuredAttachments` rather than counted as passing. `buildThreejsPhysicalAuditFeedback(physicalAudit)` turns actionable findings into default refinement feedback. |
| `threejsModelPlayerSource.js` | `THREEJS_PLAYER_SOURCE` — the fixed clip-player source `buildThreejsFactorySource` emits into every exported Three.js module, giving a standalone consumer `createSculptAnimationPlayer(root, { onCue })` (plus `evaluateSculptClipPose` / `collectSculptCues`) over the node map and validated `animation` block the factory already carries. A STRING constant, not generated text: nothing provider-authored is interpolated into it, so the export stays data-plus-PortOS-code. It takes `update(deltaSeconds)` from the host render loop rather than owning one, scrubs silently and fires cues only on playback (mirroring the preview), and clones a shared material before driving `opacity`. Semantics mirror `client/src/lib/threejsAnimation.js` — change one and change the other. |
| `threejsModelRig.js` | `evaluateThreejsRigReadiness(spec)` — honest rig-readiness report over an already-validated Three.js spec: `{ articulationReady, reasons, jointCount, socketCount, attachmentCount, anchoredAttachmentCount, unanchoredAttachmentCount, rootJointId, subjectType }`. The schema proves the optional `articulation` graph is well *formed* (one root, no cycles or forward refs, joints and pivots pointed at real parts/sockets); this reports whether it is *useful* (more than a lone root, every child joint carrying a pivot axis, every declared attachment naming what it hangs from) and names the reason when it is not. It reports rather than rejects, and never claims skinning: PortOS generates static assemblies and declared articulation intent, not skeletons or bind poses. |
| `pgFileFacade.js` | Shared PG/file store-backend backbone for the six storage dispatchers (pipeline series/issues, story builder, universe builder, catalog user-types, writers room). `isFileBackend()` (dev/test escape-hatch predicate) · `resolvePgBackend({ requirement, migrate?, loadDb, makePg })` (health-check → `ensureSchema` → one-time migration → import `db.js` → build the PG backend) · `createPgFileFacade({ makeFile, makePg })` (promise-memoized lazy selection so concurrent first calls don't run the migration twice; returns `{ getBackend, getBackendName, reset }`). Each store keeps its own `makeFile`/`makePg` factories + public surface. `createRecordStoreBackendSelector({ label, loadFileBackend, loadDbBackend, requireDbMessage?, isTestMode?, onDbReady? })` wraps the same backbone for the stores whose backends are whole MODULES rather than built objects (Creative Director, Music Video, Sprites) → `{ selectBackend, getBackendName }` where the name is `'file'`/`'postgres'`; `isTestMode` lets a store use the stronger `isTestRunner()` signal (Sprites). |
diff --git a/server/lib/threejsModelPhysicalAudit.js b/server/lib/threejsModelPhysicalAudit.js
index 5aca378383..e01f5c8b7d 100644
--- a/server/lib/threejsModelPhysicalAudit.js
+++ b/server/lib/threejsModelPhysicalAudit.js
@@ -9,6 +9,9 @@
* 5. `nonuniform-parent-scale`: A non-relief child inherits a parent's anisotropic scale and may be distorted.
* 6. `unanchored-attachment`: A part declared as carried that names nothing it is carried by.
* 7. `attachment-far-from-anchor`: A declared attachment measured away from the anchor the spec says it hangs from.
+ * 8. `bilateral-mirror-scale`: One half of a named left/right pair mirrored by negating a scale component.
+ * 9. `bilateral-pair-same-side`: A named left/right pair that never crosses the lateral plane.
+ * 10. `bilateral-chirality`: A named left/right pair mirrored by a 180-degree yaw instead of a lateral reflection.
*
* Checks 6 and 7 exist because `floating-part` and `buried-geometry` both miss
* the same defect: a hat that belongs behind the shoulders but is authored at
@@ -17,6 +20,11 @@
* relationship to its anchor makes the position wrong — which is why an
* attachment whose anchor geometry cannot be measured is reported as unmeasured
* rather than allowed to read as clean.
+ * Checks 8-10 exist because every bounds-based check above is blind to
+ * handedness: a hand spun around the vertical axis, or scaled by -1 in X,
+ * occupies exactly the same box as a correctly reflected one. Only the
+ * transform that placed it says which way round the limb is built.
+
*/
import { isThreejsAttachmentAnchored, listSpecNames, resolveThreejsAttachments } from './threejsModel.js';
@@ -29,6 +37,20 @@ const MAX_AUDIT_POSES = 16;
// catching the intentional shape-squashing that distorts a child hierarchy.
const NONUNIFORM_SCALE_TOLERANCE = 0.01;
const ANISOTROPY_COMPARISON_TOLERANCE = 0.01;
+// A mirrored limb is REFLECTED across the lateral plane (x = 0). Two other
+// transforms land the part in roughly the right place while inverting its
+// handedness: a 180-degree yaw about the vertical axis, and a negated scale
+// component. Both are what turn a right hand into a left hand with the thumb
+// pointing backward, so both are named explicitly rather than measured as a
+// generic bounds defect — the bounding boxes are identical either way.
+const LATERAL_REFLECTION = [-1, 0, 0, 0, 1, 0, 0, 0, 1];
+const VERTICAL_YAW_180 = [-1, 0, 0, 0, 1, 0, 0, 0, -1];
+const CHIRALITY_LINEAR_TOLERANCE = 1e-3;
+const CHIRALITY_POSITION_TOLERANCE = 1e-3;
+// A reflected pair is compared with a tolerance that grows with how far the
+// pair sits from the lateral plane, so a limb two units out is not reported for
+// authoring noise that a limb near the centreline would never produce.
+const CHIRALITY_POSITION_RELATIVE_TOLERANCE = 0.02;
const degreesToRadians = (degrees) => (degrees * Math.PI) / 180;
@@ -544,6 +566,207 @@ const collectAnimatedScaleSamples = (part, sequences) => sequences
{ scale: sequence.channels.scale.to, sequenceId: sequence.id },
]);
+// Side tokens are matched as whole identifier tokens, never as substrings: a
+// substring match pairs "relay" with "lay" and reads the "l" out of "lamp".
+const BILATERAL_SIDE_TOKENS = new Map([
+ ['left', 'left'],
+ ['l', 'left'],
+ ['right', 'right'],
+ ['r', 'right'],
+]);
+
+const tokenizeIdentifier = (value) => String(value ?? '')
+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
+ .split(/[^A-Za-z0-9]+/)
+ .filter(Boolean)
+ .map((token) => token.toLowerCase());
+
+/**
+ * The pairing key for one identifier: its remaining tokens, sorted, so
+ * "leftHand", "hand_L" and "Hand Left" all key to the same pair.
+ *
+ * Order is discarded deliberately. A generated spec routinely mixes
+ * conventions between the two halves of one pair — "leftFoot" beside
+ * "foot_R" — and an ordered key would leave exactly those pairs unchecked.
+ * Every other token still has to match as a multiset, so the pairing stays
+ * as tight as the names themselves.
+ *
+ * Returns null when there is no side token, when there are several (a name that
+ * says both sides names neither), or when the side token is the whole
+ * identifier — a part simply called "left" has no counterpart to be paired with.
+ */
+const describeBilateralSide = (identifier) => {
+ const tokens = tokenizeIdentifier(identifier);
+ if (tokens.length < 2) return null;
+ const sideIndexes = tokens.reduce((found, token, index) => (
+ BILATERAL_SIDE_TOKENS.has(token) ? [...found, index] : found
+ ), []);
+ if (sideIndexes.length !== 1) return null;
+ const [sideIndex] = sideIndexes;
+ return {
+ key: tokens.filter((_, index) => index !== sideIndex).sort().join('|'),
+ side: BILATERAL_SIDE_TOKENS.get(tokens[sideIndex]),
+ };
+};
+
+/**
+ * Group parts into left/right pairs by name (falling back to id).
+ *
+ * Only an unambiguous one-to-one grouping is returned. Three parts sharing a
+ * key — a spec with two "wing-l" variants — cannot say which counterpart each
+ * one is meant to mirror, and guessing would report chirality against the wrong
+ * limb.
+ */
+const collectBilateralPairs = (parts) => {
+ const groups = new Map();
+ for (const part of parts) {
+ const descriptor = describeBilateralSide(part.name || part.id)
+ || (part.name ? describeBilateralSide(part.id) : null);
+ if (!descriptor) continue;
+ const group = groups.get(descriptor.key) || { left: [], right: [] };
+ group[descriptor.side].push(part);
+ groups.set(descriptor.key, group);
+ }
+ return [...groups.values()]
+ .filter((group) => group.left.length === 1 && group.right.length === 1)
+ .map((group) => ({ left: group.left[0], right: group.right[0] }));
+};
+
+// Every primitive in this set maps onto itself under a 180-degree yaw, so a
+// lone one of them is built identically whether it was reflected or turned
+// around. Reporting those would bury the real defects under every plain box
+// limb in the catalogue. `extrude`, `tube` and `custom` carry an authored
+// profile that a yaw visibly turns around, and any assembly of more than one
+// volume shows the flip in where its pieces land.
+const YAW_SYMMETRIC_GEOMETRY_TYPES = new Set(['box', 'sphere', 'cylinder', 'cone', 'capsule', 'torus', 'lathe']);
+
+const isYawDistinguishable = (volumes, partId) => {
+ const subtree = volumes.filter((volume) => isInSubtree(volume, partId));
+ if (subtree.length !== 1) return subtree.length > 1;
+ return !YAW_SYMMETRIC_GEOMETRY_TYPES.has(subtree[0].geometry?.type);
+};
+
+const determinant3 = (m) => (
+ (m[0] * ((m[4] * m[8]) - (m[5] * m[7])))
+ - (m[1] * ((m[3] * m[8]) - (m[5] * m[6])))
+ + (m[2] * ((m[3] * m[7]) - (m[4] * m[6])))
+);
+
+const linearClose = (a, b) => a.every((value, index) => Math.abs(value - b[index]) <= CHIRALITY_LINEAR_TOLERANCE);
+
+const vectorClose = (a, b, tolerance) => a.every((value, index) => Math.abs(value - b[index]) <= tolerance);
+
+const formatVector = (vector) => `[${vector.map(formatOffset).join(', ')}]`;
+
+/**
+ * Where a part actually sits, measured over its whole subtree.
+ *
+ * A limb is routinely a bare group whose surface lives entirely in its children,
+ * so the transform origin alone would place a whole arm at the shoulder pivot
+ * and compare two pivots that are correctly mirrored while the hands hanging off
+ * them are not.
+ */
+const bilateralCenter = (volumes, part, transform) => {
+ const bounds = subtreeBounds(volumes, part.id);
+ if (!bounds) return transform.translation;
+ return bounds.map(([min, max]) => (min + max) / 2);
+};
+
+/**
+ * Chirality and symmetry findings for the resting pose.
+ *
+ * Only the resting pose is audited: which way round a limb is built is authored
+ * into the assembly, and a clip that rotates an arm is not a chirality defect.
+ *
+ * @returns {Array} findings, at most one per pair
+ */
+function buildBilateralChiralityFindings({ pairs, volumes, transformsByPartId }) {
+ const findings = [];
+
+ for (const { left, right } of pairs) {
+ const leftTransform = transformsByPartId.get(left.id);
+ const rightTransform = transformsByPartId.get(right.id);
+ if (!leftTransform || !rightTransform) continue;
+
+ const leftName = left.name || left.id;
+ const rightName = right.name || right.id;
+ const partIds = [left.id, right.id];
+ const leftCenter = bilateralCenter(volumes, left, leftTransform);
+ const rightCenter = bilateralCenter(volumes, right, rightTransform);
+
+ // A negated scale component is the one mirror that leaves the bounds
+ // untouched and every normal inside out, so it is reported before the
+ // orientation comparison — which has no meaning across a handedness flip.
+ const leftDeterminant = determinant3(leftTransform.linear);
+ const rightDeterminant = determinant3(rightTransform.linear);
+ if ((leftDeterminant < 0) !== (rightDeterminant < 0)) {
+ const flipped = leftDeterminant < 0 ? leftName : rightName;
+ const intact = leftDeterminant < 0 ? rightName : leftName;
+ findings.push({
+ code: 'bilateral-mirror-scale',
+ severity: 'warning',
+ partIds,
+ message: `Bilateral pair "${leftName}" / "${rightName}" is mirrored by negating a scale component on "${flipped}", which inverts its handedness and face winding relative to "${intact}". Build the pair with positive scale on both sides and reflect the counterpart across the lateral plane instead: position [-x, y, z] with rotation [rx, -ry, -rz].`,
+ });
+ continue;
+ }
+
+ // A pair that never crosses the lateral plane was not mirrored at all — one
+ // limb was copied to the other side's name and left where it was.
+ const sameSide = Math.sign(leftCenter[0]) === Math.sign(rightCenter[0])
+ && Math.min(Math.abs(leftCenter[0]), Math.abs(rightCenter[0])) > TOUCH_TOLERANCE;
+ if (sameSide) {
+ findings.push({
+ code: 'bilateral-pair-same-side',
+ severity: 'warning',
+ partIds,
+ message: `Bilateral pair "${leftName}" / "${rightName}" sits entirely on one side of the lateral plane (centred at x = ${formatOffset(leftCenter[0])} and x = ${formatOffset(rightCenter[0])}), so the pair never straddles the body. Reflect one of them across x = 0 so each limb sits on its own side.`,
+ });
+ continue;
+ }
+
+ const positionTolerance = Math.max(
+ CHIRALITY_POSITION_TOLERANCE,
+ CHIRALITY_POSITION_RELATIVE_TOLERANCE * Math.hypot(...leftCenter),
+ );
+ const reflectedCenter = [-leftCenter[0], leftCenter[1], leftCenter[2]];
+ const yawedCenter = [-leftCenter[0], leftCenter[1], -leftCenter[2]];
+ const placementReflected = vectorClose(rightCenter, reflectedCenter, positionTolerance);
+ const placementYawed = !placementReflected
+ && Math.abs(leftCenter[2]) > positionTolerance
+ && vectorClose(rightCenter, yawedCenter, positionTolerance);
+
+ const reflectedLinear = multiplyLinear(multiplyLinear(LATERAL_REFLECTION, leftTransform.linear), LATERAL_REFLECTION);
+ const orientationReflected = linearClose(rightTransform.linear, reflectedLinear);
+ // Both compositions are tested because the yaw is authored either way: as a
+ // 180-degree turn of the placed limb (world side) or as a [0, 180, 0] local
+ // rotation on the part itself (local side).
+ const orientationYawed = !orientationReflected && isYawDistinguishable(volumes, right.id) && (
+ linearClose(rightTransform.linear, multiplyLinear(VERTICAL_YAW_180, leftTransform.linear))
+ || linearClose(rightTransform.linear, multiplyLinear(leftTransform.linear, VERTICAL_YAW_180))
+ );
+
+ // Nothing is reported for a pair that is merely posed differently: an arm
+ // raised on one side is legitimate, and only a positively identified yaw is
+ // a chirality defect.
+ if (!orientationYawed && !placementYawed) continue;
+
+ const reasons = [
+ ...(orientationYawed ? [`"${rightName}" carries the orientation of "${leftName}" turned 180° about the vertical axis instead of reflected across it`] : []),
+ ...(placementYawed ? [`"${rightName}" is centred at ${formatVector(rightCenter)} where a lateral reflection of "${leftName}" would place it at ${formatVector(reflectedCenter)}`] : []),
+ ];
+
+ findings.push({
+ code: 'bilateral-chirality',
+ severity: 'warning',
+ partIds,
+ message: `Bilateral pair "${leftName}" / "${rightName}" is mirrored by a 180° yaw about the vertical axis rather than a reflection across the lateral plane: ${reasons.join('; ')}. This preserves handedness instead of inverting it, which is what produces backward-facing hands and feet with the big toe on the outer edge. Reflect the counterpart instead: position [-x, y, z] with rotation [rx, -ry, -rz] and positive scale.`,
+ });
+ }
+
+ return findings;
+}
+
/**
* @param {object|null} spec a validated Three.js scene spec
* @returns {{findings: Array, errorCount: number, warningCount: number, noteCount: number,
@@ -629,6 +852,14 @@ export function evaluateThreejsPhysicalAudit(spec) {
const visibleStaticVolumes = staticVolumes.filter((v) => v.visible && v.opacity > 0);
+ for (const finding of buildBilateralChiralityFindings({
+ pairs: collectBilateralPairs(parts),
+ volumes: visibleStaticVolumes,
+ transformsByPartId,
+ })) {
+ addFinding(finding);
+ }
+
// Floating check & Buried & Coplanar on resting pose
for (let i = 0; i < visibleStaticVolumes.length; i += 1) {
const subject = visibleStaticVolumes[i];
@@ -862,6 +1093,11 @@ export function buildThreejsPhysicalAuditFeedback(physicalAudit) {
const unmeasured = Array.isArray(physicalAudit?.unmeasuredAttachments) ? physicalAudit.unmeasuredAttachments : [];
if (actionable.length === 0 && unmeasured.length === 0) return '';
const hasNonuniformParentScale = actionable.some((finding) => finding.code === 'nonuniform-parent-scale');
+ const hasBilateralChirality = actionable.some((finding) => (
+ finding.code === 'bilateral-chirality'
+ || finding.code === 'bilateral-mirror-scale'
+ || finding.code === 'bilateral-pair-same-side'
+ ));
const attachmentFindings = actionable.filter((finding) => (
finding.code === 'unanchored-attachment' || finding.code === 'attachment-far-from-anchor'
));
@@ -878,6 +1114,9 @@ export function buildThreejsPhysicalAuditFeedback(physicalAudit) {
...(hasNonuniformParentScale ? [
'For non-uniform parent scale findings, size each part through its own geometry dimensions (for example, box width/height/depth or sphere radius) and keep containers near-uniform instead of squashing a parent that owns other components.',
] : []),
+ ...(hasBilateralChirality ? [
+ 'For bilateral pair findings, mirror a paired limb across the lateral plane rather than turning it around: give the counterpart position [-x, y, z] and rotation [rx, -ry, -rz] with every scale component positive. A 180° yaw about the vertical axis or a negative scale factor leaves the bounding box correct while pointing thumbs backward and putting big toes on the outer edge.',
+ ] : []),
...(attachmentFindings.length > 0 ? [
`For attachment findings, every entry in "articulation.attachments" must name the part or socket it hangs from and be positioned against it${anchorNames.length > 0 ? ` (the anchors named above: ${listSpecNames(anchorNames.map((name) => `"${name}"`))})` : ''}. Anchoring to the model root is not acceptable — name the body part that actually carries the piece.`,
] : []),
diff --git a/server/lib/threejsModelPhysicalAudit.test.js b/server/lib/threejsModelPhysicalAudit.test.js
index 035b9f4441..32521b1173 100644
--- a/server/lib/threejsModelPhysicalAudit.test.js
+++ b/server/lib/threejsModelPhysicalAudit.test.js
@@ -899,4 +899,118 @@ describe('threejsModelPhysicalAudit attachment anchors', () => {
const feedback = buildThreejsPhysicalAuditFeedback(res);
expect(feedback).toContain('could not be checked against "group"');
});
+
+ // Bilateral chirality is invisible to every bounds check above: a hand spun
+ // 180° about the vertical axis fills exactly the same box as a reflected one,
+ // so these cases pin the transform relationship rather than the geometry.
+ describe('bilateral chirality', () => {
+ // A swept profile, not a box: a box maps onto itself under a 180° yaw, so
+ // nothing distinguishes a reflected one from a turned-around one.
+ const hand = { type: 'tube', path: [[0, 0, -0.25], [0, 0, 0.25]], radius: 0.15 };
+ const bilateralSpec = (left, right) => ({
+ name: 'Bilateral Figure',
+ parts: [
+ {
+ id: 'torso',
+ name: 'Torso',
+ geometry: { type: 'box', width: 1, height: 2, depth: 0.6 },
+ position: [0, 1, 0],
+ children: [
+ { id: 'hand-l', name: 'Hand Left', geometry: hand, position: [0.6, 0, 0.2], ...left },
+ { id: 'hand-r', name: 'Hand Right', geometry: hand, position: [-0.6, 0, 0.2], ...right },
+ ],
+ },
+ ],
+ });
+ const codes = (spec) => evaluateThreejsPhysicalAudit(spec).findings
+ .filter((finding) => finding.code.startsWith('bilateral-'))
+ .map((finding) => finding.code);
+
+ it('accepts a pair reflected across the lateral plane', () => {
+ const spec = bilateralSpec(
+ { rotationDegrees: [10, 20, 30] },
+ { rotationDegrees: [10, -20, -30] },
+ );
+ expect(codes(spec)).toEqual([]);
+ expect(buildThreejsPhysicalAuditFeedback(evaluateThreejsPhysicalAudit(spec))).toBe('');
+ });
+
+ it('flags a pair mirrored by a 180 degree yaw instead of a reflection', () => {
+ const res = evaluateThreejsPhysicalAudit(bilateralSpec({}, { rotationDegrees: [0, 180, 0] }));
+ const finding = res.findings.find((f) => f.code === 'bilateral-chirality');
+ expect(finding.severity).toBe('warning');
+ expect(finding.partIds).toEqual(['hand-l', 'hand-r']);
+ expect(finding.message).toContain('turned 180° about the vertical axis');
+ expect(finding.message).toContain('[rx, -ry, -rz]');
+ });
+
+ it('flags a counterpart placed at the yawed depth rather than the reflected one', () => {
+ const res = evaluateThreejsPhysicalAudit(bilateralSpec({}, { position: [-0.6, 0, -0.2] }));
+ const finding = res.findings.find((f) => f.code === 'bilateral-chirality');
+ expect(finding.message).toContain('where a lateral reflection of "Hand Left" would place it');
+ });
+
+ it('flags a pair mirrored by negating a scale component', () => {
+ const res = evaluateThreejsPhysicalAudit(bilateralSpec({}, { scale: [-1, 1, 1] }));
+ const finding = res.findings.find((f) => f.code === 'bilateral-mirror-scale');
+ expect(finding.message).toContain('negating a scale component on "Hand Right"');
+ // The negated half is the one named, not whichever side happens to be first.
+ expect(finding.message).toContain('relative to "Hand Left"');
+ });
+
+ it('flags a pair that never crosses the lateral plane', () => {
+ const res = evaluateThreejsPhysicalAudit(bilateralSpec({}, { position: [0.6, 0, -1.2] }));
+ const finding = res.findings.find((f) => f.code === 'bilateral-pair-same-side');
+ expect(finding.message).toContain('sits entirely on one side of the lateral plane');
+ });
+
+ // A merely asymmetric pose is legitimate — only a positively identified yaw
+ // is a defect, or the gate would report every raised arm.
+ it('ignores a pair posed differently without a chirality flip', () => {
+ expect(codes(bilateralSpec({}, { rotationDegrees: [25, -20, -30] }))).toEqual([]);
+ });
+
+ it('pairs across naming conventions and camelCase', () => {
+ const spec = bilateralSpec({}, { rotationDegrees: [0, 180, 0] });
+ spec.parts[0].children[0].name = 'leftFoot';
+ spec.parts[0].children[1].name = 'foot_R';
+ expect(codes(spec)).toEqual(['bilateral-chirality']);
+ });
+
+ // Which counterpart a third same-side part is meant to mirror is unknowable,
+ // and guessing would report chirality against the wrong limb.
+ it('skips a group that cannot be paired one to one', () => {
+ const spec = bilateralSpec({}, { rotationDegrees: [0, 180, 0] });
+ spec.parts[0].children.push({
+ id: 'hand-l-spare', name: 'Hand Left', geometry: hand, position: [0.6, 0.8, 0.2],
+ });
+ expect(codes(spec)).toEqual([]);
+ });
+
+ // A box reads the same either way round, so the yaw is not a defect and
+ // reporting it would bury the real findings under every blocky limb.
+ it('ignores a yaw on a lone geometry that maps onto itself', () => {
+ const spec = bilateralSpec({}, { rotationDegrees: [0, 180, 0] });
+ spec.parts[0].children[0].geometry = { type: 'box', width: 0.3, height: 0.3, depth: 0.5 };
+ spec.parts[0].children[1].geometry = { type: 'box', width: 0.3, height: 0.3, depth: 0.5 };
+ expect(codes(spec)).toEqual([]);
+ });
+
+ it('ignores a name whose only token is a side word', () => {
+ const spec = bilateralSpec({}, { rotationDegrees: [0, 180, 0] });
+ spec.parts[0].children[0].name = 'Left';
+ spec.parts[0].children[0].id = 'left';
+ spec.parts[0].children[1].name = 'Right';
+ spec.parts[0].children[1].id = 'right';
+ expect(codes(spec)).toEqual([]);
+ });
+
+ it('feeds bilateral findings back with the reflection recipe', () => {
+ const feedback = buildThreejsPhysicalAuditFeedback(
+ evaluateThreejsPhysicalAudit(bilateralSpec({}, { rotationDegrees: [0, 180, 0] })),
+ );
+ expect(feedback).toContain('For bilateral pair findings');
+ expect(feedback).toContain('position [-x, y, z] and rotation [rx, -ry, -rz]');
+ });
+ });
});