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
fix: clear Moltworld presence panel on empty nearby-agents events (#2022)
Adam Eivye3d91ba1c24f
diff --git a/client/src/components/agents/tabs/WorldTab.jsx b/client/src/components/agents/tabs/WorldTab.jsxindex 7223df0e5e..506553763e 100644--- a/client/src/components/agents/tabs/WorldTab.jsx+++ b/client/src/components/agents/tabs/WorldTab.jsx@@ -383,7 +383,11 @@ export default function WorldTab({ agentId }) {const profile = status?.profile;const bal = status?.balance?.balance || status?.balance;- const displayNearby = presence.length > 0 ? presence : nearby;+ // `presence === null` = no live snapshot yet → fall back to the last REST+ // join/explore result. Once the WS has spoken (even an empty `[]`), the live+ // snapshot wins so a "nobody nearby" event clears the panel instead of+ // leaving stale phantoms.+ const displayNearby = presence !== null ? presence : nearby;const statusDotColor = {connected: 'bg-port-success',@@ -603,7 +607,7 @@ export default function WorldTab({ agentId }) {<div className="bg-port-card border border-port-border rounded-lg p-4"><h3 className="font-semibold text-white mb-3">Nearby Agents{displayNearby.length > 0 && <span className="text-gray-500 font-normal ml-2 text-sm">({displayNearby.length})</span>}- {presence.length > 0 && wsConnected && (+ {presence?.length > 0 && wsConnected && (<span className="text-xs text-port-success ml-2 font-normal">(live)</span>)}</h3>diff --git a/client/src/hooks/useMoltworldWs.js b/client/src/hooks/useMoltworldWs.jsindex 415816ab89..3b74947be2 100644--- a/client/src/hooks/useMoltworldWs.js+++ b/client/src/hooks/useMoltworldWs.js@@ -11,13 +11,16 @@ let idCounter = 0;* Subscribes to moltworld:* Socket.IO events and maintains:* - connectionStatus: 'disconnected' | 'connecting' | 'connected' | 'reconnecting'* - feedItems: ring buffer of last 200 events (newest first)- * - presence: latest agent presence snapshot+ * - presence: latest agent presence snapshot — `null` until the first+ * presence/nearby event arrives (not-yet-known), then a (possibly empty)+ * array. An empty array is a *confirmed-empty* snapshot that must clear the+ * panel, distinct from the not-yet-known `null` sentinel.* - connect(accountId) / disconnect(): control the server-side WS relay*/export default function useMoltworldWs() {const [connectionStatus, setConnectionStatus] = useState('disconnected');const [feedItems, setFeedItems] = useState([]);- const [presence, setPresence] = useState([]);+ const [presence, setPresence] = useState(null);const feedRef = useRef([]);const addFeedItem = useCallback((eventType, data) => {@@ -45,9 +48,12 @@ export default function useMoltworldWs() {addFeedItem(data.type || data.event || 'event', data);};const handlePresence = (data) => {- const agents = data.agents || data.nearby || [];- if (agents.length > 0) setPresence(agents);- addFeedItem('presence', { ...data, content: `${agents.length} agents nearby` });+ const agents = data.agents ?? data.nearby ?? [];+ // Validate shape, not length: an empty array is a legitimate+ // "nobody nearby" snapshot that must clear a previously-populated+ // panel — gating on `.length > 0` drops it and leaves phantoms.+ if (Array.isArray(agents)) setPresence(agents);+ addFeedItem('presence', { ...data, content: `${Array.isArray(agents) ? agents.length : 0} agents nearby` });};const handleThinking = (data) => {addFeedItem('thinking', data);@@ -59,9 +65,9 @@ export default function useMoltworldWs() {addFeedItem('interaction', data);};const handleNearby = (data) => {- const agents = data.agents || data.nearby || [];- if (agents.length > 0) setPresence(agents);- addFeedItem('nearby', { ...data, content: `${agents.length} agents` });+ const agents = data.agents ?? data.nearby ?? [];+ if (Array.isArray(agents)) setPresence(agents);+ addFeedItem('nearby', { ...data, content: `${Array.isArray(agents) ? agents.length : 0} agents` });};socket.on('moltworld:status', handleStatus);diff --git a/client/src/hooks/useMoltworldWs.test.jsx b/client/src/hooks/useMoltworldWs.test.jsxnew file mode 100644index 0000000000..5b2c322b1f--- /dev/null+++ b/client/src/hooks/useMoltworldWs.test.jsx@@ -0,0 +1,65 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';+import { renderHook, act, cleanup } from '@testing-library/react';++// Mock the socket module so the test can drive the `moltworld:*` handlers the+// hook registers with `on()`.+const handlers = new Map();+vi.mock('../services/socket', () => ({+ default: {+ on: (event, fn) => { handlers.set(event, fn); },+ off: (event, fn) => { if (handlers.get(event) === fn) handlers.delete(event); },+ emit: () => {},+ },+}));++// Mock the initial status fetch so mount doesn't hit the network.+vi.mock('../services/api', () => ({+ moltworldWsStatus: vi.fn(async () => ({ status: 'disconnected' })),+ moltworldWsConnect: vi.fn(async () => ({ status: 'connected' })),+ moltworldWsDisconnect: vi.fn(async () => ({ status: 'disconnected' })),+}));++const useMoltworldWs = (await import('./useMoltworldWs.js')).default;++const fire = (event, payload) => act(() => { handlers.get(event)?.(payload); });++describe('useMoltworldWs — presence empty-event transition (#2022)', () => {+ beforeEach(() => { handlers.clear(); });+ afterEach(cleanup);++ it('starts null (not-yet-known) before any presence event', () => {+ const { result } = renderHook(() => useMoltworldWs());+ expect(result.current.presence).toBeNull();+ });++ it('clears the presence list when an empty presence event follows a populated one', () => {+ const { result } = renderHook(() => useMoltworldWs());++ fire('moltworld:presence', { agents: [{ id: 'a1', name: 'Alice' }, { id: 'a2', name: 'Bob' }] });+ expect(result.current.presence).toHaveLength(2);++ fire('moltworld:presence', { agents: [] });+ expect(result.current.presence).toEqual([]);+ });++ it('clears the presence list on an empty nearby event too', () => {+ const { result } = renderHook(() => useMoltworldWs());++ fire('moltworld:nearby', { nearby: [{ id: 'a1', name: 'Alice' }] });+ expect(result.current.presence).toHaveLength(1);++ fire('moltworld:nearby', { nearby: [] });+ expect(result.current.presence).toEqual([]);+ });++ it('ignores a malformed (non-array) presence payload, preserving prior state', () => {+ const { result } = renderHook(() => useMoltworldWs());++ fire('moltworld:presence', { agents: [{ id: 'a1', name: 'Alice' }] });+ expect(result.current.presence).toHaveLength(1);++ // A well-formed empty array clears; a non-array payload must not.+ fire('moltworld:presence', { agents: 'oops' });+ expect(result.current.presence).toHaveLength(1);+ });+});