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

fix: address review — fail loudly on a non-finite targetHeight instead of writing NaN through the transform (#4119)

[._.]/ Adam Eivyf40efcc13819
diff --git a/client/src/utils/README.md b/client/src/utils/README.md
index 9396ee637b..a08e56a3e2 100644
--- a/client/src/utils/README.md
+++ b/client/src/utils/README.md
@@ -36,7 +36,7 @@ grep -i "what you want to do" client/src/utils/README.md
| `coalesce` | Trailing-edge coalescer: wraps a function so rapid calls collapse into one deferred invocation. |
| `easing` | `smoothstep` interpolation easing curve. |
| `hashString` | Deterministic string → 32-bit hash (stable colors, keys, seeds). |
-| `modelFit` | `fitModelToHeight(object, { targetHeight, feetOnGround, yOffset })` — normalize a loaded GLB to a fixed on-screen height and anchor it vertically, either by bounding-box center (portrait framing) or by its lowest point (a figure standing on a ground plane). Recenters x/z, guards a zero-height model against an infinite scale, skips a geometry-less object entirely, and resets to identity before measuring so a repeat call (StrictMode's double mount effect) converges instead of blowing the model back up. Call from an effect, never during render — a skinned mesh measures wrong before `useAnimations` binds the skeleton. |
+| `modelFit` | `fitModelToHeight(object, { targetHeight, feetOnGround, yOffset })` — normalize a loaded GLB to a fixed on-screen height and anchor it vertically, either by bounding-box center (portrait framing) or by its lowest point (a figure standing on a ground plane). Recenters x/z, guards a zero-height model against an infinite scale, leaves a geometry-less object or a non-finite `targetHeight` untouched rather than writing `NaN`/`Infinity` through the transform, and resets to identity before measuring so a repeat call (StrictMode's double mount effect) converges instead of blowing the model back up. Call from an effect, never during render — a skinned mesh measures wrong before `useAnimations` binds the skeleton. |
| `sleep` | `sleep(ms)` — promise-returning `setTimeout` for retry backoffs and race timeouts. Use instead of re-declaring a local `delay`. |
| `urlNormalize` | `isUrl` detection, `normalizeUrl` (optional git/`requireDot` modes), `isHttpUrl` (explicit http(s) only — safe-href check), and `tiktokVideoId` / `tiktokEmbedSrc` (host-anchored TikTok video-id extraction + its Embed Player URL, so a reference embeds without loading TikTok's embed.js). |
| `platform` | `isMac` detection and `modKey` (⌘/Ctrl) for keyboard-shortcut display. |
diff --git a/client/src/utils/modelFit.js b/client/src/utils/modelFit.js
index 855e740eef..dd339a2624 100644
--- a/client/src/utils/modelFit.js
+++ b/client/src/utils/modelFit.js
@@ -21,7 +21,21 @@ import * as THREE from 'three';
// measure an already-fitted model, compute scale ≈ 1, and blow it back up to
// source size). GLTFLoader's scene root is a plain identity-transform Group, so
// resetting it discards nothing the model authored.
-export function fitModelToHeight(object, { targetHeight, feetOnGround = false, yOffset = 0 } = {}) {
+//
+// `Box3.setFromObject` measures in WORLD space while `scale`/`position` are
+// written in the object's own local space, so the fit folds in whatever the
+// parent chain contributes at the moment it runs. That has always been true of
+// the avatars' hand-rolled fits, each of which parents its scene under a group
+// it drives per frame — call this on a scene whose parent is at (or near)
+// identity, or accept that the parent's transform is baked into the result.
+export function fitModelToHeight(object, { targetHeight, feetOnGround = false, yOffset = 0 }) {
+ // A missing or garbage height would silently write NaN through the transform
+ // and drop the object out of the scene with no clue why. Say so and bail.
+ if (!Number.isFinite(targetHeight)) {
+ console.error(`❌ fitModelToHeight needs a finite targetHeight, got ${targetHeight}`);
+ return;
+ }
+
object.scale.setScalar(1);
object.position.set(0, 0, 0);
object.updateMatrixWorld(true);
diff --git a/client/src/utils/modelFit.test.js b/client/src/utils/modelFit.test.js
index 1b9ead0952..07ced89cc6 100644
--- a/client/src/utils/modelFit.test.js
+++ b/client/src/utils/modelFit.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi, afterEach } from 'vitest';
import * as THREE from 'three';
import { fitModelToHeight } from './modelFit.js';
@@ -97,4 +97,19 @@ describe('fitModelToHeight', () => {
expect(empty.position.toArray()).toEqual([0, 0, 0]);
expect(empty.scale.toArray()).toEqual([1, 1, 1]);
});
+
+ describe('with no usable targetHeight', () => {
+ afterEach(() => vi.restoreAllMocks());
+
+ // A bad height would otherwise divide into NaN and write NaN through scale
+ // and position, dropping the model out of the scene with nothing logged.
+ it.each([undefined, null, Number.NaN, 'tall'])('leaves the model untouched and says so (%s)', (targetHeight) => {
+ const onError = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const model = makeModel({ height: 8, offset: [2, 5, 1] });
+ fitModelToHeight(model, { targetHeight, feetOnGround: true });
+ expect(model.position.toArray()).toEqual([0, 0, 0]);
+ expect(model.scale.toArray()).toEqual([1, 1, 1]);
+ expect(onError).toHaveBeenCalledTimes(1);
+ });
+ });
});