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): re-frame on clip change and fire every cue once across a long loop gap (#4542)

[._.]/ Adam Eivy97fd431c1d1a
diff --git a/client/src/components/threejsModels/ThreejsModelPreview.jsx b/client/src/components/threejsModels/ThreejsModelPreview.jsx
index bf311ed430..18b73316e7 100644
--- a/client/src/components/threejsModels/ThreejsModelPreview.jsx
+++ b/client/src/components/threejsModels/ThreejsModelPreview.jsx
@@ -224,11 +224,17 @@ function Part({ part, materials, layout, selection, selectedId, onSelect, auditM
// without remounting anything, so re-fit whenever the layout ACTUALLY grew —
// measured from the moved parts, not guessed from the slider — and the camera
// frames the disassembly instead of clipping it.
-function ExplodeRefit({ growth }) {
+//
+// Opening a different clip is the other discrete moment worth re-framing on: its
+// starting pose can sit well outside the assembled one. Playing or scrubbing is
+// deliberately NOT one — re-fitting on every posed frame turns the camera into
+// something that chases the mechanism, which is far worse to watch than a part
+// that swings past the edge of a frame the viewer chose.
+function SceneRefit({ growth, clipId }) {
const bounds = useBounds();
useEffect(() => {
bounds?.refresh().clip().fit();
- }, [bounds, growth]);
+ }, [bounds, growth, clipId]);
return null;
}
@@ -283,13 +289,13 @@ function SceneLight({ light }) {
return <directionalLight color={light.color} intensity={light.intensity} position={light.position} castShadow />;
}
-function ProceduralScene({ spec, background, layout, selection, selectedId, onSelect, auditMode, pose }) {
+function ProceduralScene({ spec, background, layout, selection, selectedId, onSelect, auditMode, pose, clipId }) {
return (
<>
{background && <color attach="background" args={[background]} />}
{spec.lights.map((light, index) => <SceneLight key={`${light.type}-${index}`} light={light} />)}
<Bounds fit clip observe margin={1.25}>
- <ExplodeRefit growth={layout.growth} />
+ <SceneRefit growth={layout.growth} clipId={clipId} />
{/* Clicking past the model clears the selection, the way a file list
does — but only when there is one, so a stray click on empty canvas
doesn't push a URL write and re-render the page around us. */}
@@ -387,9 +393,18 @@ function useClipPlayback({ clip, cuesById, onCue }) {
fire(from, raw);
setPlayhead(raw);
} else if (clip.loop) {
- fire(from, duration);
const wrapped = raw % duration;
- fire(0, wrapped);
+ if (raw - from >= duration) {
+ // A frame gap longer than the whole clip — a backgrounded tab, a stall
+ // — crossed every cue in it. Fire each of them ONCE for the gap
+ // instead of replaying a backlog per skipped cycle: a resumed tab must
+ // not burst N copies of the same sound, and dropping the skipped
+ // cycles entirely would silently under-fire the contract.
+ fire(0, duration);
+ } else {
+ fire(from, duration);
+ fire(0, wrapped);
+ }
setPlayhead(wrapped);
} else {
fire(from, duration);
@@ -553,6 +568,7 @@ export default function ThreejsModelPreview({ spec, family = null, className = '
onSelect={handleSelect}
auditMode={auditMode}
pose={pose}
+ clipId={clip?.id || null}
/>
</Canvas>
<div className="port-media-overlay absolute left-2 top-2 flex max-w-[calc(100%-1rem)] flex-wrap items-center gap-1.5 rounded-lg px-2 py-1.5 text-[10px]">
diff --git a/client/src/components/threejsModels/ThreejsModelPreview.test.jsx b/client/src/components/threejsModels/ThreejsModelPreview.test.jsx
index 79459cf7f4..edc7b5984c 100644
--- a/client/src/components/threejsModels/ThreejsModelPreview.test.jsx
+++ b/client/src/components/threejsModels/ThreejsModelPreview.test.jsx
@@ -632,3 +632,65 @@ describe('ThreejsModelPreview clips', () => {
expect(screen.getByText('Blade')).toBeInTheDocument();
});
});
+
+describe('ThreejsModelPreview clip framing and looping', () => {
+ let frames = [];
+ let nowMs = 0;
+
+ beforeEach(() => {
+ frames = [];
+ nowMs = 0;
+ boundsApi.fit.mockClear();
+ vi.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((callback) => frames.push(callback));
+ vi.spyOn(globalThis, 'cancelAnimationFrame').mockImplementation(() => {});
+ vi.spyOn(performance, 'now').mockImplementation(() => nowMs);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ const advance = (ms) => {
+ nowMs += ms;
+ const pending = frames.splice(0);
+ act(() => {
+ for (const callback of pending) callback(nowMs);
+ });
+ };
+
+ it('re-frames when a different clip opens, but never chases a playing one', () => {
+ renderPreview(<ThreejsModelPreview spec={clipSpec()} />);
+ boundsApi.fit.mockClear();
+
+ // Scrubbing and playing move parts every frame — a camera that re-fit on
+ // each of them would chase the mechanism instead of watching it.
+ scrub(0.5);
+ fireEvent.click(screen.getByLabelText('Play clip'));
+ advance(300);
+ expect(boundsApi.fit).not.toHaveBeenCalled();
+
+ // Opening another clip is a discrete jump to a pose that can sit well
+ // outside the assembled one, so that one does re-frame.
+ fireEvent.change(screen.getByLabelText('Clip'), { target: { value: 'retract' } });
+ expect(boundsApi.fit).toHaveBeenCalled();
+ });
+
+ it('fires every cue once across a frame gap longer than a looping clip', () => {
+ const spec = clipSpec();
+ spec.animation.clips[0].loop = true;
+ const onCue = vi.fn();
+ renderPreview(<ThreejsModelPreview spec={spec} onCue={onCue} />);
+
+ fireEvent.click(screen.getByLabelText('Play clip'));
+ // 7s on a 2s loop: three and a half cycles skipped in one frame. Every cue
+ // fires once for the gap — not once per skipped cycle, and not zero times.
+ advance(7_000);
+ expect(onCue).toHaveBeenCalledTimes(1);
+ expect(onCue).toHaveBeenCalledWith(expect.objectContaining({ cueId: 'latchRelease' }));
+ // ...and the clip keeps running from where it wrapped to.
+ expect(screen.getByText('1.00/2.00s')).toBeInTheDocument();
+
+ advance(1_500);
+ expect(onCue).toHaveBeenCalledTimes(2);
+ });
+});