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

refactor([issue-705]): extract shared hashString util, dedup three copies

[._.]/ Adam Eivycbbc92bb76bc
diff --git a/client/src/components/city/Borough.jsx b/client/src/components/city/Borough.jsx
index b0a0780624..0314567c97 100644
--- a/client/src/components/city/Borough.jsx
+++ b/client/src/components/city/Borough.jsx
@@ -1,18 +1,10 @@
import { useMemo } from 'react';
import { getBuildingHeight, BOROUGH_PARAMS } from './cityConstants';
+import { hashString } from '../../utils/hashString';
import Building from './Building';
import AgentEntity from './AgentEntity';
import ProcessBuilding from './ProcessBuilding';
-const hashString = (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);
-};
-
export default function Borough({ app, position, agentMap, onBuildingClick, playSfx, neonBrightness, isProximity, dimmed = false, settings }) {
const agentData = agentMap.get(app.id);
const agents = agentData?.agents || [];
diff --git a/client/src/components/city/cityConstants.js b/client/src/components/city/cityConstants.js
index b771967e2b..db06b40b57 100644
--- a/client/src/components/city/cityConstants.js
+++ b/client/src/components/city/cityConstants.js
@@ -1,3 +1,5 @@
+import { hashString } from '../../utils/hashString';
+
// Geist Pixel Square font URL for drei <Text> in 3D scene (TTF required, troika doesn't support woff2)
export const PIXEL_FONT_URL = '/fonts/GeistPixel-Square.ttf';
@@ -235,16 +237,6 @@ export const DISTRICT_PARAMS = {
gap: 4,
};
-// Simple string hash for consistent per-app randomness
-const hashString = (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);
-};
-
export const getBuildingColor = (status, archived) => {
if (archived) return CITY_COLORS.building.archived;
return CITY_COLORS.building[status] || CITY_COLORS.building.not_started;
diff --git a/client/src/utils/cityFlowLines.js b/client/src/utils/cityFlowLines.js
index 256ecbb685..b0dd62634d 100644
--- a/client/src/utils/cityFlowLines.js
+++ b/client/src/utils/cityFlowLines.js
@@ -6,6 +6,8 @@
// No three.js / React imports here so the topology can be unit-tested in
// isolation (mirrors the cityTimeline.js / cityAgentMotion.js helper pattern).
+import { hashString } from './hashString';
+
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)
@@ -16,18 +18,6 @@ export const FLOW = {
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)
@@ -67,8 +57,8 @@ export function computeFlowConnections({ positions, activeIds, agentIds, maxNeig
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
+ const hot = agentIds?.has(a.id) || agentIds?.has(b.id) || false;
+ const variation = (hashString(key) % 100) / 100; // 0..1, deterministic per link
conns.push({
key,
start: [a.x, 0.5, a.z],
diff --git a/client/src/utils/cityFlowLines.test.js b/client/src/utils/cityFlowLines.test.js
index 79ed8e105d..61b152998e 100644
--- a/client/src/utils/cityFlowLines.test.js
+++ b/client/src/utils/cityFlowLines.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { FLOW, hashFlowKey, computeFlowConnections } from './cityFlowLines';
+import { FLOW, computeFlowConnections } from './cityFlowLines';
// Four downtown buildings in a row plus one non-downtown and one offscreen.
const positions = new Map([
@@ -10,13 +10,6 @@ const positions = new Map([
['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([]);
diff --git a/client/src/utils/hashString.js b/client/src/utils/hashString.js
new file mode 100644
index 0000000000..de71daebd1
--- /dev/null
+++ b/client/src/utils/hashString.js
@@ -0,0 +1,11 @@
+// Stable, non-negative DJB2-style string hash. Used to derive deterministic
+// per-entity variation (process seeds, neon accent colors, flow-link jitter)
+// without Math.random, so renders are reproducible across reloads and frames.
+export function hashString(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);
+}
diff --git a/client/src/utils/hashString.test.js b/client/src/utils/hashString.test.js
new file mode 100644
index 0000000000..3b55fedb25
Binary files /dev/null and b/client/src/utils/hashString.test.js differ