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
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 everysocket.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>:detachedon the previous socket when a new socket takes over so the displaced client can drop its local view; (2) include anattached: booleanfield on each list-entry payload, computed relative to the recipient socket (true only when bound to a different socket) — a globally-truthyattachedmakes a client's own sessions look unavailable to itself; (3) broadcast list updates from both attach AND detach paths; (4) auto-pick paths sendclaim: trueand the server refuses to displace a different socket. Manual paths (tab click, deep-link URL) default toclaim: falseso explicit intent still wins. Seeserver/services/shell.jsfor the canonical implementation.Pending socket-request tracking —
{ target, generation }ref. When a stateful socket operation is in flight, track it as{ target, generation }and incrementgenerationon every change. Response handlers gate on strict equality withtarget— null/stale/cancelled all fall through, so a cancelled-mid-flight response can't re-activate after the user moved on. Deferred work (setTimeoutfallbacks) capturesgenerationand aborts if it advanced. Pair every cancellation path with explicitcancelPendingAttach()-style helpers rather than overloading aclearActiveSession()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. Seeclient/src/hooks/useShellSession.jspendingAttachReffor the pattern.Server-correlate every async response, then filter display. When the server emits
<resource>:errorin response to a client request, include the originalsessionId/ 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, expectedclaim:truerace rejections). Passive errors against the currently-displayed resource (e.g.shell:inputto 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*Refflag 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) amountedRefso a navigation-away unmount stops it from firing into the void. UseuseMounted()fromclient/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.jsrejects both auseRef(true)that is only ever set tofalseand 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.