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
feat([issue-705]): give CyberCity agents orbiting motion trails
[._.]/ Adam Eivy26a4beefa69e
diff --git a/client/src/components/city/AgentEntity.jsx b/client/src/components/city/AgentEntity.jsxindex 9fcce6fbca..354c507bb8 100644--- a/client/src/components/city/AgentEntity.jsx+++ b/client/src/components/city/AgentEntity.jsx@@ -1,56 +1,105 @@-import { useRef } from 'react';+import { useRef, useMemo } from 'react';import { useFrame } from '@react-three/fiber';import { Sparkles } from '@react-three/drei';+import * as THREE from 'three';import { AGENT_STATES } from '../cos/constants';+import {+ computeAgentOrbit,+ computeAgentTrailPoints,+ computeTrailColors,+ resolveTrailSamples,+} from '../../utils/cityAgentMotion';const DEFAULT_COLOR = '#06b6d4';-export default function AgentEntity({ agent, position, index = 0 }) {- const meshRef = useRef();+export default function AgentEntity({ agent, position, index = 0, settings }) {+ const bodyRef = useRef();+ const trailRef = useRef();const state = agent.state || agent.status || 'coding';const color = AGENT_STATES[state]?.color || DEFAULT_COLOR;- const offsetX = index * 1.0 - (index > 0 ? 0.5 : 0);- const yBase = position[1];+ // Trail density follows the quality dial; 0 means "don't render a trail".+ const trailSamples = resolveTrailSamples(settings?.particleDensity ?? 1);++ // Static geometry sized to the sample count; positions stream in per frame,+ // the color ramp (head→tail fade) is baked once.+ const trailGeom = useMemo(() => {+ const geom = new THREE.BufferGeometry();+ if (trailSamples > 0) {+ const c = new THREE.Color(color);+ geom.setAttribute(+ 'position',+ new THREE.Float32BufferAttribute(new Float32Array(trailSamples * 3), 3),+ );+ geom.setAttribute(+ 'color',+ new THREE.Float32BufferAttribute(+ new Float32Array(computeTrailColors([c.r, c.g, c.b], trailSamples)),+ 3,+ ),+ );+ }+ return geom;+ }, [color, trailSamples]);++ // The group sits at the building anchor; the agent body orbits within it so+ // its motion trail samples in anchor-relative space.+ const anchor = [position[0], position[1], position[2]];useFrame(({ clock }) => {- if (!meshRef.current) return;const t = clock.getElapsedTime();+ const p = computeAgentOrbit(t, { index });- // Vertical bob- meshRef.current.position.y = yBase + Math.sin(t * 1.5 + index) * 0.3;-- // Y rotation- meshRef.current.rotation.y = t * 0.8 + index * Math.PI * 0.5;+ if (bodyRef.current) {+ bodyRef.current.position.set(p.x, p.y, p.z);+ bodyRef.current.rotation.y = t * 0.8 + index * Math.PI * 0.5;+ const material = bodyRef.current.children[0]?.material;+ if (material) material.emissiveIntensity = 0.5 + Math.sin(t * 3) * 0.3;+ }- // Emissive pulse- meshRef.current.children[0]?.material && (- meshRef.current.children[0].material.emissiveIntensity = 0.5 + Math.sin(t * 3) * 0.3- );+ if (trailRef.current && trailSamples > 0) {+ const pts = computeAgentTrailPoints(t, { index }, trailSamples);+ const attr = trailRef.current.geometry.getAttribute('position');+ attr.array.set(pts);+ attr.needsUpdate = true;+ }});return (- <group ref={meshRef} position={[position[0] + offsetX, yBase, position[2]]}>- <mesh>- <octahedronGeometry args={[0.3, 0]} />- <meshStandardMaterial+ <group position={anchor}>+ {trailSamples > 0 && (+ <line ref={trailRef} geometry={trailGeom}>+ <lineBasicMaterial+ vertexColors+ transparent+ opacity={0.7}+ blending={THREE.AdditiveBlending}+ depthWrite={false}+ />+ </line>+ )}+ <group ref={bodyRef}>+ <mesh>+ <octahedronGeometry args={[0.3, 0]} />+ <meshStandardMaterial+ color={color}+ emissive={color}+ emissiveIntensity={0.5}+ wireframe+ transparent+ opacity={0.8}+ />+ </mesh>+ <Sparkles+ count={15}+ scale={0.8}+ size={1}+ speed={0.5}color={color}- emissive={color}- emissiveIntensity={0.5}- wireframe- transparent- opacity={0.8}+ opacity={0.6}/>- </mesh>- <Sparkles- count={15}- scale={0.8}- size={1}- speed={0.5}- color={color}- opacity={0.6}- />+ </group></group>);}diff --git a/client/src/components/city/Borough.jsx b/client/src/components/city/Borough.jsxindex a05524da43..b0a0780624 100644--- a/client/src/components/city/Borough.jsx+++ b/client/src/components/city/Borough.jsx@@ -13,7 +13,7 @@ const hashString = (str) => {return Math.abs(hash);};-export default function Borough({ app, position, agentMap, onBuildingClick, playSfx, neonBrightness, isProximity, dimmed = false }) {+export default function Borough({ app, position, agentMap, onBuildingClick, playSfx, neonBrightness, isProximity, dimmed = false, settings }) {const agentData = agentMap.get(app.id);const agents = agentData?.agents || [];const height = getBuildingHeight(app);@@ -75,6 +75,7 @@ export default function Borough({ app, position, agentMap, onBuildingClick, playagent={agent}position={[position.x, height + 1.5 + i * 1.0, position.z]}index={i}+ settings={settings}/>))}</group>diff --git a/client/src/components/city/BuildingCluster.jsx b/client/src/components/city/BuildingCluster.jsxindex aa0608e39f..d5f5b8761d 100644--- a/client/src/components/city/BuildingCluster.jsx+++ b/client/src/components/city/BuildingCluster.jsx@@ -39,6 +39,7 @@ export default function BuildingCluster({ apps, agentMap, onBuildingClick, onPosneonBrightness={settings?.neonBrightness ?? 1.2}isProximity={proximityAppId === app.id}dimmed={dimmedAppIds?.has?.(app.id) || false}+ settings={settings}/>);})}diff --git a/client/src/utils/cityAgentMotion.js b/client/src/utils/cityAgentMotion.jsnew file mode 100644index 0000000000..af620d5e38--- /dev/null+++ b/client/src/utils/cityAgentMotion.js@@ -0,0 +1,76 @@+// Pure, deterministic helpers for animating CyberCity agent entities along an+// orbit path and rendering a fading motion trail behind them. No three.js /+// React imports here so the geometry math can be unit-tested in isolation+// (mirrors the cityTimeline.js helper pattern).++export const AGENT_MOTION = {+ orbitRadius: 0.9, // horizontal orbit radius around the building anchor+ orbitSpeed: 0.6, // radians/sec around the anchor+ bobAmp: 0.3, // vertical bob amplitude (matches the legacy AgentEntity bob)+ bobSpeed: 1.5, // vertical bob speed+ trailSeconds: 1.6, // how far back in time the trail samples+ trailSamples: 24, // points along the trail at full quality+};++// Position of an agent at elapsed time `t` (seconds), as an offset relative to+// its building anchor. `index` fans multiple agents on the same building into+// distinct orbit phases so their paths and trails don't overlap.+export function computeAgentOrbit(t, { index = 0, radius, orbitSpeed, bobAmp, bobSpeed } = {}) {+ const r = radius ?? AGENT_MOTION.orbitRadius;+ const os = orbitSpeed ?? AGENT_MOTION.orbitSpeed;+ const ba = bobAmp ?? AGENT_MOTION.bobAmp;+ const bs = bobSpeed ?? AGENT_MOTION.bobSpeed;+ const phase = index * (Math.PI * 0.5); // quarter-turn fan per agent+ const angle = t * os + phase;+ return {+ x: Math.cos(angle) * r,+ y: Math.sin(t * bs + index) * ba,+ z: Math.sin(angle) * r,+ };+}++// Resolve how many trail samples to draw for a given quality density.+// `particleDensity` ranges ~0.5 (low) .. 1.5 (ultra). At/above the low floor it+// scales linearly to the full sample count; below the floor the trail is+// dropped entirely (returns 0) so weak hardware pays nothing for it.+export function resolveTrailSamples(particleDensity = 1, maxSamples = AGENT_MOTION.trailSamples) {+ if (!(particleDensity >= 0.5)) return 0;+ const scaled = Math.round((maxSamples * Math.min(1.5, particleDensity)) / 1.5);+ return Math.max(2, scaled);+}++// Sample the orbit path backwards from time `t` into `samples` points, newest+// first. Returns a flat [x0,y0,z0, x1,y1,z1, ...] array of offsets relative to+// the anchor — the caller adds the anchor position when placing the trail.+export function computeAgentTrailPoints(+ t,+ opts = {},+ samples = AGENT_MOTION.trailSamples,+ trailSeconds = AGENT_MOTION.trailSeconds,+) {+ const n = Math.max(2, samples);+ const pts = new Array(n * 3);+ for (let i = 0; i < n; i++) {+ const dt = (i / (n - 1)) * trailSeconds; // 0 at head (newest), trailSeconds at tail+ const p = computeAgentOrbit(t - dt, opts);+ pts[i * 3] = p.x;+ pts[i * 3 + 1] = p.y;+ pts[i * 3 + 2] = p.z;+ }+ return pts;+}++// Per-vertex color ramp aligned with computeAgentTrailPoints: full color at the+// head (newest) fading to black at the tail. `rgb` is a [r,g,b] triple in 0..1.+// Pairs with an additive-blended lineBasicMaterial so black reads as transparent.+export function computeTrailColors(rgb, samples = AGENT_MOTION.trailSamples) {+ const n = Math.max(2, samples);+ const colors = new Array(n * 3);+ for (let i = 0; i < n; i++) {+ const fade = 1 - i / (n - 1); // 1 at head .. 0 at tail+ colors[i * 3] = rgb[0] * fade;+ colors[i * 3 + 1] = rgb[1] * fade;+ colors[i * 3 + 2] = rgb[2] * fade;+ }+ return colors;+}diff --git a/client/src/utils/cityAgentMotion.test.js b/client/src/utils/cityAgentMotion.test.jsnew file mode 100644index 0000000000..af79749d94--- /dev/null+++ b/client/src/utils/cityAgentMotion.test.js@@ -0,0 +1,133 @@+import { describe, it, expect } from 'vitest';+import {+ AGENT_MOTION,+ computeAgentOrbit,+ resolveTrailSamples,+ computeAgentTrailPoints,+ computeTrailColors,+} from './cityAgentMotion';++describe('computeAgentOrbit', () => {+ it('places the agent on a circle of the configured radius (ignoring bob)', () => {+ const { x, z } = computeAgentOrbit(0, { radius: 2, bobAmp: 0 });+ expect(Math.hypot(x, z)).toBeCloseTo(2, 6);+ });++ it('at t=0 with no bob sits at angle 0 (x=radius, z=0)', () => {+ const p = computeAgentOrbit(0, { radius: 1, bobAmp: 0 });+ expect(p.x).toBeCloseTo(1, 6);+ expect(p.z).toBeCloseTo(0, 6);+ expect(p.y).toBeCloseTo(0, 6);+ });++ it('advances around the circle as time progresses', () => {+ const a = computeAgentOrbit(0, { radius: 1, orbitSpeed: 1, bobAmp: 0 });+ const b = computeAgentOrbit(Math.PI / 2, { radius: 1, orbitSpeed: 1, bobAmp: 0 });+ // quarter turn: x→0, z→1+ expect(b.x).toBeCloseTo(0, 6);+ expect(b.z).toBeCloseTo(1, 6);+ expect(a.x).not.toBeCloseTo(b.x, 3);+ });++ it('fans agents on the same building into distinct phases by index', () => {+ const a0 = computeAgentOrbit(0, { radius: 1, bobAmp: 0, index: 0 });+ const a1 = computeAgentOrbit(0, { radius: 1, bobAmp: 0, index: 1 });+ expect(a0.x).not.toBeCloseTo(a1.x, 3);+ });++ it('applies vertical bob within the configured amplitude', () => {+ let maxY = 0;+ for (let t = 0; t < 20; t += 0.05) {+ maxY = Math.max(maxY, Math.abs(computeAgentOrbit(t, { bobAmp: 0.3 }).y));+ }+ expect(maxY).toBeGreaterThan(0.25);+ expect(maxY).toBeLessThanOrEqual(0.3 + 1e-9);+ });++ it('falls back to AGENT_MOTION defaults when opts are omitted', () => {+ const { x, z } = computeAgentOrbit(0);+ expect(Math.hypot(x, z)).toBeCloseTo(AGENT_MOTION.orbitRadius, 6);+ });+});++describe('resolveTrailSamples', () => {+ it('drops the trail entirely below the low-quality floor', () => {+ expect(resolveTrailSamples(0)).toBe(0);+ expect(resolveTrailSamples(0.4)).toBe(0);+ });++ it('renders a short trail at the low preset (0.5)', () => {+ expect(resolveTrailSamples(0.5, 24)).toBe(8);+ });++ it('scales up to the full sample count at ultra (1.5)', () => {+ expect(resolveTrailSamples(1.5, 24)).toBe(24);+ expect(resolveTrailSamples(1.0, 24)).toBe(16);+ });++ it('clamps densities above ultra to the max sample count', () => {+ expect(resolveTrailSamples(5, 24)).toBe(24);+ });++ it('never returns fewer than 2 points for a renderable trail', () => {+ expect(resolveTrailSamples(0.5, 2)).toBe(2);+ });+});++describe('computeAgentTrailPoints', () => {+ it('returns samples*3 flat coordinates', () => {+ const pts = computeAgentTrailPoints(1, {}, 10);+ expect(pts).toHaveLength(30);+ });++ it('head (first point) matches the current orbit position', () => {+ const t = 3.21;+ const opts = { index: 2 };+ const head = computeAgentOrbit(t, opts);+ const pts = computeAgentTrailPoints(t, opts, 12);+ expect(pts[0]).toBeCloseTo(head.x, 6);+ expect(pts[1]).toBeCloseTo(head.y, 6);+ expect(pts[2]).toBeCloseTo(head.z, 6);+ });++ it('tail (last point) matches the orbit position trailSeconds in the past', () => {+ const t = 3.21;+ const opts = { index: 0 };+ const tail = computeAgentOrbit(t - AGENT_MOTION.trailSeconds, opts);+ const pts = computeAgentTrailPoints(t, opts, 8, AGENT_MOTION.trailSeconds);+ const last = pts.length - 3;+ expect(pts[last]).toBeCloseTo(tail.x, 6);+ expect(pts[last + 2]).toBeCloseTo(tail.z, 6);+ });++ it('clamps to a minimum of 2 points', () => {+ expect(computeAgentTrailPoints(0, {}, 1)).toHaveLength(6);+ });+});++describe('computeTrailColors', () => {+ it('returns samples*3 channel values', () => {+ expect(computeTrailColors([1, 0.5, 0.25], 10)).toHaveLength(30);+ });++ it('is full color at the head and black at the tail', () => {+ const rgb = [0.2, 0.4, 0.8];+ const colors = computeTrailColors(rgb, 5);+ expect(colors[0]).toBeCloseTo(rgb[0], 6);+ expect(colors[1]).toBeCloseTo(rgb[1], 6);+ expect(colors[2]).toBeCloseTo(rgb[2], 6);+ const last = colors.length - 3;+ expect(colors[last]).toBeCloseTo(0, 6);+ expect(colors[last + 1]).toBeCloseTo(0, 6);+ expect(colors[last + 2]).toBeCloseTo(0, 6);+ });++ it('fades monotonically from head to tail', () => {+ const colors = computeTrailColors([1, 1, 1], 6);+ const reds = [];+ for (let i = 0; i < colors.length; i += 3) reds.push(colors[i]);+ for (let i = 1; i < reds.length; i++) {+ expect(reds[i]).toBeLessThan(reds[i - 1]);+ }+ });+});