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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: measure bilateral chirality in the pair's own parent frame, not against world x = 0
[._.]/ Adam Eivy0d54ba78c01e
diff --git a/server/lib/README.md b/server/lib/README.mdindex a8bf93239c..a5975a901e 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. 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. |+| `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 — measured in the frame of the pair’s nearest common ancestor, since the lateral plane a pair mirrors across is the one their shared parent defines and not world `x = 0` — 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.jsindex e01f5c8b7d..1d7534ad96 100644--- a/server/lib/threejsModelPhysicalAudit.js+++ b/server/lib/threejsModelPhysicalAudit.js@@ -575,21 +575,24 @@ const BILATERAL_SIDE_TOKENS = new Map([['r', 'right'],]);+// Both camel boundaries, not just the common one: "handL" needs lower-upper,+// and "LHand" needs upper-upper-lower or the whole name reads as one token and+// the pair is never found.const tokenizeIdentifier = (value) => String(value ?? '').replace(/([a-z0-9])([A-Z])/g, '$1 $2')+ .replace(/([A-Z]+)([A-Z][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.+ * "leftHand", "hand_L", "LHand" 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.+ * 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.** 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@@ -632,18 +635,42 @@ const collectBilateralPairs = (parts) => {.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 collectPartChains = (parts) => {+ const chains = new Map();+ const walk = (list, ancestors) => {+ for (const part of list || []) {+ const chain = [...ancestors, part];+ chains.set(part.id, chain);+ walk(part.children, chain);+ }+ };+ walk(parts, []);+ return chains;+};-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);+/**+ * Both halves of a pair placed in the frame of their nearest common ancestor.+ *+ * The lateral plane a pair is mirrored across is the one their shared parent+ * defines, NOT world x = 0. A subject modelled off the origin, or a body yawed+ * to face somewhere other than down +Z, is mirror-symmetric about its own axis+ * and about no world plane at all — comparing it against x = 0 reports every+ * correctly built pair on it. Composing down from the common ancestor cancels+ * that shared placement, and leaves exactly the authored local transforms the+ * refinement feedback asks the next pass to change.+ */+const transformsRelativeToCommonAncestor = (leftChain, rightChain) => {+ let shared = 0;+ while (shared < leftChain.length - 1+ && shared < rightChain.length - 1+ && leftChain[shared] === rightChain[shared]) {+ shared += 1;+ }+ const compose = (chain) => chain.slice(shared).reduce(+ (transform, part) => composeTransform(transform, part.position, part.rotationDegrees, part.scale),+ IDENTITY_TRANSFORM,+ );+ return [compose(leftChain), compose(rightChain)];};const determinant3 = (m) => (@@ -658,18 +685,18 @@ const vectorClose = (a, b, tolerance) => a.every((value, index) => Math.abs(valuconst 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);+// 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 blocky 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);};/**@@ -678,21 +705,29 @@ const bilateralCenter = (volumes, part, transform) => {* 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.*+ * A flipped ORIENTATION is what every finding here turns on. A counterpart+ * merely placed at a different depth is a staggered pose — a figure mid-stride+ * has one hand forward and one back — and reporting that as chirality would ask+ * the next pass to flatten every pose it was asked for. The depth is reported+ * only as corroboration, once the orientation already says the limb is turned+ * around.+ ** @returns {Array} findings, at most one per pair*/-function buildBilateralChiralityFindings({ pairs, volumes, transformsByPartId }) {+function buildBilateralChiralityFindings({ pairs, chains, volumes }) {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 leftChain = chains.get(left.id);+ const rightChain = chains.get(right.id);+ if (!leftChain || !rightChain) continue;+ const [leftTransform, rightTransform] = transformsRelativeToCommonAncestor(leftChain, rightChain);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);+ const leftPosition = leftTransform.translation;+ const rightPosition = rightTransform.translation;// A negated scale component is the one mirror that leaves the bounds// untouched and every normal inside out, so it is reported before the@@ -711,56 +746,48 @@ function buildBilateralChiralityFindings({ pairs, volumes, transformsByPartId })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;+ // A pair that never crosses its parent's 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(leftPosition[0]) === Math.sign(rightPosition[0])+ && Math.min(Math.abs(leftPosition[0]), Math.abs(rightPosition[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.`,+ message: `Bilateral pair "${leftName}" / "${rightName}" sits entirely on one side of the lateral plane (both offset from their common parent at x = ${formatOffset(leftPosition[0])} and x = ${formatOffset(rightPosition[0])}), so the pair never straddles the body. Negate the x offset of one of them 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) && (+ if (linearClose(rightTransform.linear, reflectedLinear)) continue;+ // 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. 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 = isYawDistinguishable(volumes, right.id) && (linearClose(rightTransform.linear, multiplyLinear(VERTICAL_YAW_180, leftTransform.linear))|| linearClose(rightTransform.linear, multiplyLinear(leftTransform.linear, VERTICAL_YAW_180)));+ if (!orientationYawed) continue;- // 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)}`] : []),- ];+ const positionTolerance = Math.max(+ CHIRALITY_POSITION_TOLERANCE,+ CHIRALITY_POSITION_RELATIVE_TOLERANCE * Math.hypot(...leftPosition),+ );+ const reflectedPosition = [-leftPosition[0], leftPosition[1], leftPosition[2]];+ const yawedPosition = [-leftPosition[0], leftPosition[1], -leftPosition[2]];+ const placementYawed = !vectorClose(rightPosition, reflectedPosition, positionTolerance)+ && Math.abs(leftPosition[2]) > positionTolerance+ && vectorClose(rightPosition, yawedPosition, positionTolerance);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.`,+ message: `Bilateral pair "${leftName}" / "${rightName}" is mirrored by a 180° yaw about the vertical axis rather than a reflection across the lateral plane: "${rightName}" carries the orientation of "${leftName}" turned around rather than reflected${placementYawed ? `, and sits at ${formatVector(rightPosition)} where a reflection would place it at ${formatVector(reflectedPosition)}` : ''}. A yaw 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.`,});}@@ -854,8 +881,8 @@ export function evaluateThreejsPhysicalAudit(spec) {for (const finding of buildBilateralChiralityFindings({pairs: collectBilateralPairs(parts),+ chains: collectPartChains(spec.parts),volumes: visibleStaticVolumes,- transformsByPartId,})) {addFinding(finding);}diff --git a/server/lib/threejsModelPhysicalAudit.test.js b/server/lib/threejsModelPhysicalAudit.test.jsindex 32521b1173..9fd3b663a2 100644--- a/server/lib/threejsModelPhysicalAudit.test.js+++ b/server/lib/threejsModelPhysicalAudit.test.js@@ -940,14 +940,25 @@ describe('threejsModelPhysicalAudit attachment anchors', () => {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('carries the orientation of "Hand Left" turned around');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');+ // A figure mid-stride has one hand forward and one back. Depth alone is a+ // pose, and reporting it would ask the next pass to flatten every pose.+ it('ignores a staggered pose that only differs in depth', () => {+ expect(codes(bilateralSpec({}, { position: [-0.6, 0, -0.2] }))).toEqual([]);+ });++ // Depth is reported once the orientation already says the limb is turned+ // around, because it says where the reflected limb should have gone.+ it('names the reflected placement alongside a yawed orientation', () => {+ const res = evaluateThreejsPhysicalAudit(bilateralSpec(+ {},+ { position: [-0.6, 0, -0.2], rotationDegrees: [0, 180, 0] },+ ));+ const finding = res.findings.find((finding) => finding.code === 'bilateral-chirality');+ expect(finding.message).toContain('where a reflection would place it at [-0.6, 0, 0.2]');});it('flags a pair mirrored by negating a scale component', () => {@@ -1005,6 +1016,35 @@ describe('threejsModelPhysicalAudit attachment anchors', () => {expect(codes(spec)).toEqual([]);});+ // The lateral plane a pair mirrors across is the one their shared parent+ // defines, never world x = 0 — a subject modelled off the origin or facing+ // anywhere but down +Z is symmetric about its own axis and no world plane.+ it('measures a pair against its parent rather than world x = 0', () => {+ const spec = bilateralSpec({}, {});+ spec.parts[0].position = [3, 1, 0];+ expect(codes(spec)).toEqual([]);+ });++ it('measures a pair inside a yawed parent in the parent frame', () => {+ const spec = bilateralSpec(+ { rotationDegrees: [0, 90, 0] },+ { rotationDegrees: [0, -90, 0] },+ );+ spec.parts[0].rotationDegrees = [0, 35, 0];+ expect(codes(spec)).toEqual([]);+ });++ // Both id and name, or the pair resolves through the id fallback and never+ // exercises the upper-upper-lower camel split this case exists for.+ it('pairs a single-letter side prefix run onto the name', () => {+ const spec = bilateralSpec({}, { rotationDegrees: [0, 180, 0] });+ spec.parts[0].children[0].id = 'LHand';+ spec.parts[0].children[0].name = 'LHand';+ spec.parts[0].children[1].id = 'RHand';+ spec.parts[0].children[1].name = 'RHand';+ expect(codes(spec)).toEqual(['bilateral-chirality']);+ });+it('feeds bilateral findings back with the reflection recipe', () => {const feedback = buildThreejsPhysicalAuditFeedback(evaluateThreejsPhysicalAudit(bilateralSpec({}, { rotationDegrees: [0, 180, 0] })),