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

4.9 KB
Raw Blame

name: portos-socket-ui description: Socket.IO lifecycle conventions for PortOS client views — event-driven state swaps, single-subscriber resources (PTY shells, agent runs), pending-request tracking, stale-response filtering, and deferred-work guards. Invoke before wiring or debugging a socket-driven UI.

Socket-driven UI conventions

These are hard-won failure contracts from the Shell and CoS-agent views. Every one of them exists because a simpler approach shipped first and broke.

  • Socket event-driven state — don't pre-clear before the server confirms. When swapping which entity a socket-driven UI is showing (shell session, agent run, etc.), wait for the server's success event to drive the swap atomically; the error event is the recovery point. Pre-clearing the local *Ref / visible state up front leaves the UI stranded on a dead URL when the success event never arrives (target died mid-request, server rejected the switch). Pair every socket.on('X:error') that follows a stateful request with a recovery branch that restores the previously-displayed state or falls back to a live alternative.

  • Single-subscriber socket resources need notify + recipient-relative advertise + filter + claim. Some server-side resources (PTY shell sessions, etc.) intentionally store one attached socket and fan output to it. The contract: (1) emit <resource>:detached on the previous socket when a new socket takes over so the displaced client can drop its local view; (2) include an attached: boolean field on each list-entry payload, computed relative to the recipient socket (true only when bound to a different socket) — a globally-truthy attached makes a client's own sessions look unavailable to itself; (3) broadcast list updates from both attach AND detach paths; (4) auto-pick paths send claim: true and the server refuses to displace a different socket. Manual paths (tab click, deep-link URL) default to claim: false so explicit intent still wins. See server/services/shell.js for the canonical implementation.

  • Pending socket-request tracking — { target, generation } ref. When a stateful socket operation is in flight, track it as { target, generation } and increment generation on every change. Response handlers gate on strict equality with target — null/stale/cancelled all fall through, so a cancelled-mid-flight response can't re-activate after the user moved on. Deferred work (setTimeout fallbacks) captures generation and aborts if it advanced. Pair every cancellation path with explicit cancelPendingAttach()-style helpers rather than overloading a clearActiveSession() helper — clearing the displayed entity and cancelling an in-flight request are separate concerns, and conflating them cancels user-initiated switches when an unrelated session dies. See client/src/hooks/useShellSession.js pendingAttachRef for the pattern.

  • Server-correlate every async response, then filter display. When the server emits <resource>:error in response to a client request, include the original sessionId / request id in the payload so the client can match against its pending state. Drop stale errors silently and gate the red-error display on correlation — rendering before classification flashes noise in the UI for requests the user has already moved past (rapid tab clicks, expected claim:true race rejections). Passive errors against the currently-displayed resource (e.g. shell:input to a now-dead session) should still display, but must not mutate pending state.

  • Distinguish intentional idle from passive idle. A "no entity displayed" state can come from a user action (Stop / dismiss) or from passive circumstance (initial load found everything in-use elsewhere). Recovery branches that auto-adopt the next free entity must gate on a userIdle*Ref flag set by explicit user-clear paths and cleared by every user-initiated start/attach. The gate needs to cover every reconnect-triggered re-init path, not just the initial-load branch — a transient disconnect resets initialization flags, and an empty-list auto-start or survivor adoption can otherwise undo an explicit Stop on reconnect.

  • Deferred work must respect both staleness and unmount. Any setTimeout-scheduled side effect that emits to the network or mutates shared state needs two guards: (1) a generation counter check so user actions during the delay window abort it, and (2) a mountedRef so a navigation-away unmount stops it from firing into the void. Use useMounted() from client/src/hooks/useMounted.js; it re-arms on every mount, which StrictMode's mount-cleanup-remount cycle requires. Do not hand-roll the guard even correctly — client/src/hooks/mountedRefConventions.test.js rejects both a useRef(true) that is only ever set to false and any re-implementation of the hook. Without the unmount guard, a deferred socket emit can claim a resource (e.g. shell session) with no listener left to render it.