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([issue-705]): drive CyberCity flow lines from real activity

[._.]/ Adam Eivyc8fca43aaff8
diff --git a/client/src/components/city/CityDataStreams.jsx b/client/src/components/city/CityDataStreams.jsx
index f542e5e075..9ef4e43380 100644
--- a/client/src/components/city/CityDataStreams.jsx
+++ b/client/src/components/city/CityDataStreams.jsx
@@ -1,7 +1,7 @@
-import { useRef, useMemo } from 'react';
+import { useRef, useMemo, useEffect } from 'react';
import { useFrame } from '@react-three/fiber';
import * as THREE from 'three';
-import { CITY_COLORS } from './cityConstants';
+import { computeFlowConnections } from '../../utils/cityFlowLines';
// A single animated data packet traveling along a path
function DataPacket({ start, end, color, speed, offset, size = 0.08 }) {
@@ -21,6 +21,10 @@ function DataPacket({ start, end, color, speed, offset, size = 0.08 }) {
return new THREE.BufferGeometry().setFromPoints(curve.getPoints(20));
}, [start, end]);
+ // Dispose the connection-line geometry's GPU buffers when the endpoints change
+ // (topology shifts as buildings go on/offline) or on unmount.
+ useEffect(() => () => trailGeom.dispose(), [trailGeom]);
+
useFrame(({ clock }) => {
if (!meshRef.current) return;
const t = ((clock.getElapsedTime() * speed + offset) % 1.0);
@@ -56,51 +60,21 @@ function DataPacket({ start, end, color, speed, offset, size = 0.08 }) {
);
}
-export default function CityDataStreams({ positions }) {
- // Build connections between nearby active buildings
+export default function CityDataStreams({ positions, apps, agentMap }) {
+ // Derive the real operational state the flow topology is built from: which
+ // buildings are online (flow sources) and which currently have running agents
+ // (hot links). The pure helper turns that into the connection set so this
+ // component stays presentation-only.
const connections = useMemo(() => {
- if (!positions || positions.size < 2) return [];
-
- const entries = [];
- positions.forEach((pos, id) => {
- if (pos.district === 'downtown') entries.push({ id, ...pos });
+ const activeIds = new Set(
+ (apps || []).filter(a => !a.archived && a.overallStatus === 'online').map(a => a.id)
+ );
+ const agentIds = new Set();
+ agentMap?.forEach((entry, id) => {
+ if (entry?.agents?.length) agentIds.add(id);
});
-
- const conns = [];
- const colors = CITY_COLORS.neonAccents;
-
- // Connect each building to its nearest 1-2 neighbors
- for (let i = 0; i < entries.length; i++) {
- const a = entries[i];
- const distances = [];
-
- for (let j = 0; j < entries.length; j++) {
- if (i === j) continue;
- const b = entries[j];
- const dist = Math.sqrt((a.x - b.x) ** 2 + (a.z - b.z) ** 2);
- distances.push({ index: j, dist });
- }
-
- distances.sort((d1, d2) => d1.dist - d2.dist);
- const neighborCount = Math.min(1, distances.length);
-
- for (let n = 0; n < neighborCount; n++) {
- const b = entries[distances[n].index];
- // Avoid duplicate connections
- const key = [a.id, b.id].sort().join('-');
- if (!conns.find(c => c.key === key)) {
- conns.push({
- key,
- start: [a.x, 0.5, a.z],
- end: [b.x, 0.5, b.z],
- color: colors[(i + n) % colors.length],
- });
- }
- }
- }
-
- return conns;
- }, [positions]);
+ return computeFlowConnections({ positions, activeIds, agentIds });
+ }, [positions, apps, agentMap]);
if (connections.length === 0) return null;
@@ -108,21 +82,27 @@ export default function CityDataStreams({ positions }) {
<group>
{connections.map((conn) => (
<group key={conn.key}>
- {/* Two packets going each direction at different speeds */}
- <DataPacket
- start={conn.start}
- end={conn.end}
- color={conn.color}
- speed={0.15 + Math.random() * 0.1}
- offset={0}
- />
- <DataPacket
- start={conn.end}
- end={conn.start}
- color={conn.color}
- speed={0.12 + Math.random() * 0.1}
- offset={0.5}
- />
+ {/* Packets travel both directions; a hotter link carries more of them. */}
+ {Array.from({ length: conn.packets }).map((_, k) => (
+ <DataPacket
+ key={`fwd-${k}`}
+ start={conn.start}
+ end={conn.end}
+ color={conn.color}
+ speed={conn.speed}
+ offset={k / conn.packets}
+ />
+ ))}
+ {Array.from({ length: conn.packets }).map((_, k) => (
+ <DataPacket
+ key={`rev-${k}`}
+ start={conn.end}
+ end={conn.start}
+ color={conn.color}
+ speed={conn.speed}
+ offset={k / conn.packets + 0.5}
+ />
+ ))}
</group>
))}
</group>
diff --git a/client/src/components/city/CityScene.jsx b/client/src/components/city/CityScene.jsx
index c1fcfaf797..d37a03a9bf 100644
--- a/client/src/components/city/CityScene.jsx
+++ b/client/src/components/city/CityScene.jsx
@@ -84,7 +84,7 @@ export default function CityScene({ apps, agentMap, onBuildingClick, cosStatus,
proximityAppId={proximityApp?.id}
dimmedAppIds={dimmedAppIds}
/>
- <CityDataStreams positions={positions} />
+ <CityDataStreams positions={positions} apps={apps} agentMap={agentMap} />
<CityTraffic positions={positions} />
<CityBillboards
positions={positions}
diff --git a/client/src/utils/cityFlowLines.js b/client/src/utils/cityFlowLines.js
new file mode 100644
index 0000000000..256ecbb685
--- /dev/null
+++ b/client/src/utils/cityFlowLines.js
@@ -0,0 +1,85 @@
+// Pure, deterministic helpers for CyberCity's inter-building "flow lines": which
+// active buildings connect to each other, and how intense each stream is. The
+// topology is derived from real operational state — a building is a flow source
+// only when it's online, and a link runs "hot" when either endpoint has a
+// running agent — rather than the previous random nearest-neighbor decoration.
+// No three.js / React imports here so the topology can be unit-tested in
+// isolation (mirrors the cityTimeline.js / cityAgentMotion.js helper pattern).
+
+export const FLOW = {
+ maxNeighbors: 2, // connect each active building to up to N nearest active neighbors
+ hotColor: '#22d3ee', // links touching a building with a running agent (work in flight)
+ idleColor: '#3b82f6', // links between plain-online buildings (steady-state traffic)
+ basePackets: 1, // packets travelling each direction on a steady link
+ hotPackets: 2, // packets each direction when an endpoint has a running agent
+ baseSpeed: 0.12, // packet speed on a steady link (fraction of the path per second)
+ hotSpeedBonus: 0.06, // extra speed on a hot link — work-in-flight reads as faster traffic
+};
+
+// Stable non-negative hash of a string (same algorithm as Borough.jsx), used to
+// give each connection deterministic per-link variation without Math.random so
+// the topology is reproducible and testable.
+export function hashFlowKey(str) {
+ let hash = 0;
+ for (let i = 0; i < str.length; i++) {
+ hash = ((hash << 5) - hash) + str.charCodeAt(i);
+ hash |= 0;
+ }
+ return Math.abs(hash);
+}
+
+// Build the flow-line connection set between ACTIVE downtown buildings.
+// positions — Map<id, { x, z, district }> from the city layout
+// activeIds — Set<id> of buildings that are online (flow sources/sinks)
+// agentIds — Set<id> of buildings that currently have >=1 running agent
+// Returns deterministic descriptors: { key, start:[x,y,z], end:[x,y,z], color,
+// hot, packets, speed }. `hot`/color/packets/speed encode the link's intensity
+// (color = type, packets+speed = volume) so the renderer stays presentation-only.
+export function computeFlowConnections({ positions, activeIds, agentIds, maxNeighbors = FLOW.maxNeighbors } = {}) {
+ if (!positions || !activeIds || activeIds.size < 2) return [];
+
+ const entries = [];
+ positions.forEach((pos, id) => {
+ if (pos.district === 'downtown' && activeIds.has(id)) {
+ entries.push({ id, x: pos.x, z: pos.z });
+ }
+ });
+ if (entries.length < 2) return [];
+
+ const conns = [];
+ const seen = new Set();
+
+ for (let i = 0; i < entries.length; i++) {
+ const a = entries[i];
+ const neighbors = [];
+ for (let j = 0; j < entries.length; j++) {
+ if (i === j) continue;
+ const b = entries[j];
+ const dist = (a.x - b.x) ** 2 + (a.z - b.z) ** 2;
+ neighbors.push({ id: b.id, x: b.x, z: b.z, dist });
+ }
+ neighbors.sort((m, n) => m.dist - n.dist);
+
+ const take = Math.min(maxNeighbors, neighbors.length);
+ for (let n = 0; n < take; n++) {
+ const b = neighbors[n];
+ const key = [a.id, b.id].sort().join('→');
+ if (seen.has(key)) continue;
+ seen.add(key);
+
+ const hot = (agentIds?.has(a.id) || agentIds?.has(b.id)) || false;
+ const variation = (hashFlowKey(key) % 100) / 100; // 0..1, deterministic per link
+ conns.push({
+ key,
+ start: [a.x, 0.5, a.z],
+ end: [b.x, 0.5, b.z],
+ color: hot ? FLOW.hotColor : FLOW.idleColor,
+ hot,
+ packets: hot ? FLOW.hotPackets : FLOW.basePackets,
+ speed: FLOW.baseSpeed + (hot ? FLOW.hotSpeedBonus : 0) + variation * 0.05,
+ });
+ }
+ }
+
+ return conns;
+}
diff --git a/client/src/utils/cityFlowLines.test.js b/client/src/utils/cityFlowLines.test.js
new file mode 100644
index 0000000000..79ed8e105d
--- /dev/null
+++ b/client/src/utils/cityFlowLines.test.js
@@ -0,0 +1,96 @@
+import { describe, it, expect } from 'vitest';
+import { FLOW, hashFlowKey, computeFlowConnections } from './cityFlowLines';
+
+// Four downtown buildings in a row plus one non-downtown and one offscreen.
+const positions = new Map([
+ ['a', { x: 0, z: 0, district: 'downtown' }],
+ ['b', { x: 2, z: 0, district: 'downtown' }],
+ ['c', { x: 4, z: 0, district: 'downtown' }],
+ ['d', { x: 6, z: 0, district: 'downtown' }],
+ ['w', { x: 0, z: 20, district: 'warehouse' }],
+]);
+
+describe('hashFlowKey', () => {
+ it('is deterministic and non-negative', () => {
+ expect(hashFlowKey('a→b')).toBe(hashFlowKey('a→b'));
+ expect(hashFlowKey('a→b')).toBeGreaterThanOrEqual(0);
+ });
+});
+
+describe('computeFlowConnections', () => {
+ it('returns nothing with fewer than two active buildings', () => {
+ expect(computeFlowConnections({ positions, activeIds: new Set(['a']), agentIds: new Set() })).toEqual([]);
+ expect(computeFlowConnections({ positions, activeIds: new Set(), agentIds: new Set() })).toEqual([]);
+ expect(computeFlowConnections({})).toEqual([]);
+ });
+
+ it('only connects buildings that are active (online)', () => {
+ const conns = computeFlowConnections({
+ positions,
+ activeIds: new Set(['a', 'b']),
+ agentIds: new Set(),
+ });
+ expect(conns).toHaveLength(1);
+ expect(conns[0].key).toBe('a→b');
+ });
+
+ it('excludes non-downtown districts even when marked active', () => {
+ const conns = computeFlowConnections({
+ positions,
+ activeIds: new Set(['a', 'w']),
+ agentIds: new Set(),
+ });
+ // 'w' is warehouse district → not a flow source, leaving <2 downtown actives
+ expect(conns).toEqual([]);
+ });
+
+ it('connects each active building to up to maxNeighbors nearest active neighbors, deduped', () => {
+ const conns = computeFlowConnections({
+ positions,
+ activeIds: new Set(['a', 'b', 'c', 'd']),
+ agentIds: new Set(),
+ maxNeighbors: 2,
+ });
+ const keys = conns.map(c => c.key).sort();
+ // a↔b, b↔c, c↔d (adjacent), plus a→c and b→d as second-nearest — deduped both ways
+ expect(new Set(keys).size).toBe(keys.length); // no duplicate keys
+ expect(keys).toContain('a→b');
+ expect(keys).toContain('c→d');
+ // never a self-link
+ expect(keys.every(k => k.split('→')[0] !== k.split('→')[1])).toBe(true);
+ });
+
+ it('marks a link hot (agent color, more+faster packets) when either endpoint has a running agent', () => {
+ const conns = computeFlowConnections({
+ positions,
+ activeIds: new Set(['a', 'b']),
+ agentIds: new Set(['b']),
+ });
+ expect(conns[0].hot).toBe(true);
+ expect(conns[0].color).toBe(FLOW.hotColor);
+ expect(conns[0].packets).toBe(FLOW.hotPackets);
+ expect(conns[0].speed).toBeGreaterThan(FLOW.baseSpeed);
+ });
+
+ it('marks a link idle (steady color, base packets) when neither endpoint has an agent', () => {
+ const conns = computeFlowConnections({
+ positions,
+ activeIds: new Set(['a', 'b']),
+ agentIds: new Set(['c']), // agent on an unrelated building
+ });
+ expect(conns[0].hot).toBe(false);
+ expect(conns[0].color).toBe(FLOW.idleColor);
+ expect(conns[0].packets).toBe(FLOW.basePackets);
+ });
+
+ it('produces a deterministic topology across calls', () => {
+ const args = { positions, activeIds: new Set(['a', 'b', 'c']), agentIds: new Set(['a']) };
+ expect(computeFlowConnections(args)).toEqual(computeFlowConnections(args));
+ });
+
+ it('places stream endpoints at the buildings xz with a fixed y', () => {
+ const conns = computeFlowConnections({ positions, activeIds: new Set(['a', 'b']), agentIds: new Set() });
+ expect(conns[0].start).toEqual([0, 0.5, 0]);
+ expect(conns[0].end).toEqual([2, 0.5, 0]);
+ });
+});