The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
refactor: extract WebSocket liveness logic into a shared utility module
0xken15b56f26c46f27
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex 8e8259827..331d5ff3f 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'import { readFileSync } from 'fs'import { fileURLToPath } from 'url'import { BackendSocket, compactRuntimePickerModels, deviceAgentListItem, grokHistoryPage } from './backendSocket.js'+import { WS_IDLE_DEADLINE_MS as IDLE_DEADLINE_MS } from './lib/wsLiveness.js'import type { TerminalStreamManager } from './lib/terminalStreamManager.js'import { decodeTerminalLocal, TerminalBinaryKind } from './lib/terminalBinary.js'import { registry, type RegisteredSession } from './lib/registry.js'@@ -17,6 +18,9 @@ const wsMock = vi.hoisted(() => {readyState = MockWebSocket.CONNECTINGsent: string[] = []failNextSend: Error | null = null+ /** Peer stopped answering pings (a half-open TCP link, a laptop coming back from sleep). */+ silent = false+ pings = 0private handlers = new Map<string, Array<(...args: unknown[]) => void>>()constructor(readonly url: string, readonly protocols: string[], readonly options?: { handshakeTimeout?: number }) {@@ -30,6 +34,15 @@ const wsMock = vi.hoisted(() => {return this}+ once(event: string, cb: (...args: unknown[]) => void): this {+ const wrapped = (...args: unknown[]): void => {+ const list = this.handlers.get(event) ?? []+ this.handlers.set(event, list.filter((fn) => fn !== wrapped))+ cb(...args)+ }+ return this.on(event, wrapped)+ }+private emit(event: string, ...args: unknown[]): void {for (const cb of this.handlers.get(event) ?? []) cb(...args)}@@ -70,7 +83,13 @@ const wsMock = vi.hoisted(() => {}ping(): void {- this.emit('pong')+ this.pings+++ if (!this.silent) this.emit('pong')+ }++ /** The backend's own liveness ping (every 25s from `trackSocketLiveness`). */+ peerPing(): void {+ this.emit('ping')}}@@ -137,6 +156,53 @@ describe('BackendSocket outbound queue', () => {await socket.stop()})+ it('gives a silent peer the whole deadline, not one missed pong, before terminating', async () => {+ // The old heartbeat killed the link the first time a ping went unanswered (20–40s), which is+ // what every macOS DarkWake looked like from inside the daemon: the pre-sleep ping's pong never+ // came, so the first tick after wake terminated a link that was about to work again.+ vi.useFakeTimers()+ const socket = new BackendSocket('token')+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ ws.silent = true++ await vi.advanceTimersByTimeAsync(IDLE_DEADLINE_MS - 1_000)+ expect(socket.isConnected()).toBe(true)+ expect(ws.pings).toBeGreaterThanOrEqual(2) // it kept asking the whole time+ expect(wsMock.instances).toHaveLength(1)++ await vi.advanceTimersByTimeAsync(20_000)+ expect(socket.isConnected()).toBe(false)+ await vi.advanceTimersByTimeAsync(1_000)+ expect(wsMock.instances).toHaveLength(2) // and re-entered the ordinary backoff++ await socket.stop()+ })++ it('counts data and the backend\'s own pings as proof of life, not only pongs', async () => {+ // A backend busy streaming data can answer a control-frame ping late; the data itself is the+ // stronger proof, and the backend pings this socket every 25s on its own.+ vi.useFakeTimers()+ const socket = new BackendSocket('token')+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ ws.silent = true++ await vi.advanceTimersByTimeAsync(40_000)+ ws.message({ t: 'down', connId: 'c1', frame: { type: 'noop' } })+ await vi.advanceTimersByTimeAsync(40_000) // 80s in, 40s since the last frame+ expect(socket.isConnected()).toBe(true)+ ws.peerPing()+ await vi.advanceTimersByTimeAsync(40_000)+ expect(socket.isConnected()).toBe(true)+ await vi.advanceTimersByTimeAsync(IDLE_DEADLINE_MS)+ expect(socket.isConnected()).toBe(false)++ await socket.stop()+ })+it('keeps a frame queued when ws.send reports an error and retries after reconnect', async () => {vi.useFakeTimers()const socket = new BackendSocket('token')diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 39b3776ab..de339e6e0 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -8,13 +8,15 @@ import type { AutonomousDeviceService, AutonomousDeviceFrame } from './lib/auton* - `down` { t:'down', connId, frame } → web chat/control + data-plane RPC requests** Mirrors the hosted runtime’s managerSocket: idempotent connect, exponential backoff (1s→30s),- * WS ping/pong keep-alive + a 15s app-level `{t:'ping'}` that refreshes the backend presence- * key, and a bounded FIFO queue for client-facing outbound frames.+ * WS liveness (`lib/wsLiveness.ts`: ping every 20s, 60s deadline on silence) + a 15s app-level+ * `{t:'ping'}` that refreshes the backend presence key, and a bounded FIFO queue for client-facing+ * outbound frames.** Auth: the SSO access token rides as the first WS subprotocol.*/import { WebSocket } from 'ws'+import { watchSocketLiveness, type LivenessWatch } from './lib/wsLiveness.js'import { stat, readFile } from 'fs/promises'import { isAbsolute, join } from 'path'import { hostname } from 'os'@@ -90,7 +92,6 @@ const HERMES_DB = join(env.HERMES_HOME, 'state.db')export type RecentProvider = (sessionId: string, n: number) => Array<{ kind: string; text: string; recap?: string }>-const HEARTBEAT_MS = 20_000const APP_PING_MS = 15_000// How long the opening handshake may take before the attempt is abandoned and retried. `ws` waits// forever by default, and the heartbeat below only starts on 'open' — so a TCP connection that came@@ -291,7 +292,7 @@ export class BackendSocket {private draining = falseprivate nextQueueId = 1private droppedSinceLog = 0- private heartbeat: NodeJS.Timeout | null = null+ private heartbeat: LivenessWatch | null = nullprivate appPing: NodeJS.Timeout | null = nullprivate readonly downChains = new Map<string, Promise<void>>()private readonly localClients = new Map<string, LocalClientSink>()@@ -299,7 +300,6 @@ export class BackendSocket {private readonly terminalP2p: TerminalP2pResponderPoolprivate readonly p2pPendingOpens = new Map<string, Set<string>>()private readonly p2pStreams = new Map<string, Set<string>>()- private isAlive = trueprivate onStatus: (connected: boolean) => void/** Cross-instance commander (device) client count, from backend `__clients` frames. */private commanderCount = 0@@ -570,20 +570,13 @@ export class BackendSocket {ws.on('open', () => {this.attempts = 0- this.isAlive = trueconsole.log(`[backend] connected → ${this.url}`)this.onStatus(true)this.drainQueue()- this.heartbeat = setInterval(() => {- if (!this.isAlive) {- try { ws.terminate() } catch { /* ignore */ }- return- }- this.isAlive = false- try { ws.ping() } catch { /* ignore */ }- }, HEARTBEAT_MS)- ws.on('pong', () => { this.isAlive = true })+ this.heartbeat = watchSocketLiveness(ws, {+ onIdle: (idleMs) => console.log(`[backend] no traffic for ${Math.round(idleMs / 1000)}s — terminating the link`),+ })// App-level ping refreshes the backend's presence key (TTL 30s).this.appPing = setInterval(() => this.sendBestEffort({ t: 'ping' }), APP_PING_MS)@@ -608,7 +601,7 @@ export class BackendSocket {const onGone = (why: string): void => {if (this.ws !== ws) returnthis.ws = null- if (this.heartbeat) { clearInterval(this.heartbeat); this.heartbeat = null }+ if (this.heartbeat) { this.heartbeat.stop(); this.heartbeat = null }if (this.appPing) { clearInterval(this.appPing); this.appPing = null }this.draining = false// While the backend link is down we can neither observe device presence nor deliver a card, so@@ -660,7 +653,7 @@ export class BackendSocket {async stop(): Promise<void> {this.closed = true- if (this.heartbeat) clearInterval(this.heartbeat)+ if (this.heartbeat) this.heartbeat.stop()if (this.appPing) clearInterval(this.appPing)await this.terminalStreams?.stop()await this.terminalP2p.stop()diff --git a/cli/src/lib/remoteRelay.spec.ts b/cli/src/lib/remoteRelay.spec.tsindex c0505ab5b..d918acaf6 100644--- a/cli/src/lib/remoteRelay.spec.ts+++ b/cli/src/lib/remoteRelay.spec.ts@@ -12,8 +12,7 @@ function fakeEntry(overrides: Partial<Record<string, unknown>> = {}) {sink: { sendFrame: vi.fn(() => true), sendBinary: vi.fn(() => true) },onClosed: null,lingerTimer: null,- alive: true,- heartbeatTimer: null,+ heartbeat: null,p2p: null,p2pPolicy: null,p2pPendingOpens: new Set<string>(),diff --git a/cli/src/lib/remoteRelay.ts b/cli/src/lib/remoteRelay.tsindex b7730ab08..bd32f6b61 100644--- a/cli/src/lib/remoteRelay.ts+++ b/cli/src/lib/remoteRelay.ts@@ -13,6 +13,7 @@* instead of ever reaching pipe mode.*/import { WebSocket, type RawData } from 'ws'+import { watchSocketLiveness, type LivenessWatch } from './wsLiveness.js'import type { Frame, LocalClientSink } from '../backendSocket.js'import type { AuthSessionManager } from './authSession.js'import { b64d, type Identity } from './e2ee/core.js'@@ -38,7 +39,6 @@ const LINGER_MS = 30_000// an upstream socket the backend silently dropped with no close frame — every RPC sent through it then// times out client-side forever, since nothing ever removes the dead entry to let the next select// redial. `ws.terminate()` on a missed pong forces the existing `close` handler to run cleanup.-const HEARTBEAT_MS = 20_000// A p2p attempt that never reaches 'direct', or one that did and then got demoted mid-session, both// leave the stream(s) on the ws relay with nothing to bring them back — see scheduleP2pRetry(). One@@ -53,7 +53,7 @@ const HEARTBEAT_MS = 20_000const P2P_RETRY_DELAY_MS = 60_000const P2P_RETRY_WINDOW_MS = 60 * 60 * 1000const P2P_RETRY_HOURLY_CAP = 10-// How long a stream may sit in p2pMigrating before the sweep (piggybacked on heartbeatTimer) gives up+// How long a stream may sit in p2pMigrating before the sweep (piggybacked on the liveness tick) gives up// on it and lets the ordinary demote-on-mismatch rule apply again.const P2P_MIGRATION_TTL_MS = 30_000@@ -93,8 +93,7 @@ interface Entry {sink: LocalClientSink | nullonClosed: ((code: number, reason: string) => void) | nulllingerTimer: ReturnType<typeof setTimeout> | null- alive: boolean- heartbeatTimer: ReturnType<typeof setInterval> | null+ heartbeat: LivenessWatch | nullp2p: TerminalP2pInitiator | nullp2pPolicy: TerminalP2pPolicy | nullp2pPendingOpens: Set<string>@@ -189,7 +188,7 @@ export class RemoteRelayPool {const entry = this.entries.get(machineId)if (!entry) returnthis.entries.delete(machineId)- if (entry.heartbeatTimer) clearInterval(entry.heartbeatTimer)+ entry.heartbeat?.stop()if (entry.lingerTimer) clearTimeout(entry.lingerTimer)if (entry.p2pRetryTimer) clearTimeout(entry.p2pRetryTimer)if (entry.upgradeTimer) clearTimeout(entry.upgradeTimer)@@ -268,8 +267,7 @@ export class RemoteRelayPool {sink: null,onClosed: null,lingerTimer: null,- alive: true,- heartbeatTimer: null,+ heartbeat: null,p2p: null,p2pPolicy: null,p2pPendingOpens: new Set(),@@ -450,7 +448,7 @@ export class RemoteRelayPool {})// Handshake done — from here on, a close is the entry's real end-of-life, not a handshake failure.ws.on('close', (code, reasonBuf) => {- if (entry.heartbeatTimer) clearInterval(entry.heartbeatTimer)+ entry.heartbeat?.stop()if (entry.p2pRetryTimer) clearTimeout(entry.p2pRetryTimer)if (entry.upgradeTimer) clearTimeout(entry.upgradeTimer)void entry.upgradeShadow?.stop('relay_closed', false)@@ -460,21 +458,18 @@ export class RemoteRelayPool {this.entries.delete(machineId)entry.onClosed?.(code, reasonBuf?.toString() ?? '')})- ws.on('pong', () => { entry.alive = true })- entry.heartbeatTimer = setInterval(() => {- if (!entry.alive) { ws.terminate(); return }- entry.alive = false- try { ws.ping() } catch { ws.terminate() }+ entry.heartbeat = watchSocketLiveness(ws, {+ onIdle: (idleMs) => console.log(`[relay] ${machineId.slice(0, 8)} no traffic for ${Math.round(idleMs / 1000)}s — terminating`),// Piggybacked sweep for a migration that never completed (pane closed mid-flight, responder never// answered, etc.) — no dedicated timer needed, this tick is frequent enough (20s) against the 30s TTL.- if (entry.p2pMigrating.size > 0) {+ onTick: () => {+ if (entry.p2pMigrating.size === 0) returnconst cutoff = Date.now() - P2P_MIGRATION_TTL_MSfor (const [streamId, startedAt] of entry.p2pMigrating) {if (startedAt < cutoff) entry.p2pMigrating.delete(streamId)}- }- }, HEARTBEAT_MS)- entry.heartbeatTimer.unref?.()+ },+ })this.entries.set(machineId, entry)return entry}diff --git a/cli/src/lib/wsLiveness.spec.ts b/cli/src/lib/wsLiveness.spec.tsnew file mode 100644index 000000000..77bbe5290--- /dev/null+++ b/cli/src/lib/wsLiveness.spec.ts@@ -0,0 +1,99 @@+import { afterEach, describe, expect, it, vi } from 'vitest'+import { EventEmitter } from 'node:events'+import type { WebSocket } from 'ws'+import { watchSocketLiveness, WS_HEARTBEAT_MS, WS_IDLE_DEADLINE_MS } from './wsLiveness.js'++class FakeSocket extends EventEmitter {+ pings = 0+ terminated = 0+ /** Peer stopped answering (half-open TCP, a laptop back from sleep). */+ silent = false+ pingThrows = false+ ping(): void {+ if (this.pingThrows) throw new Error('not open')+ this.pings+++ if (!this.silent) this.emit('pong')+ }+ terminate(): void { this.terminated++ }+}++const socket = () => new FakeSocket() as unknown as WebSocket & FakeSocket++afterEach(() => { vi.useRealTimers() })++describe('watchSocketLiveness', () => {+ it('keeps a responsive socket alive indefinitely, pinging on the heartbeat', async () => {+ vi.useFakeTimers()+ const ws = socket()+ watchSocketLiveness(ws)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS * 10)+ expect(ws.pings).toBe(10)+ expect(ws.terminated).toBe(0)+ })++ it('gives a silent peer the whole deadline — three pings — not one missed pong', async () => {+ vi.useFakeTimers()+ const ws = socket()+ ws.silent = true+ const idle: number[] = []+ watchSocketLiveness(ws, { onIdle: (ms) => idle.push(ms) })+ await vi.advanceTimersByTimeAsync(WS_IDLE_DEADLINE_MS - 1)+ expect(ws.terminated).toBe(0)+ expect(ws.pings).toBe(2)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS)+ expect(ws.terminated).toBe(1)+ expect(idle).toEqual([WS_IDLE_DEADLINE_MS])+ })++ it('counts data and the peer\'s own pings as proof of life, not only pongs', async () => {+ vi.useFakeTimers()+ const ws = socket()+ ws.silent = true+ watchSocketLiveness(ws)+ await vi.advanceTimersByTimeAsync(40_000)+ ws.emit('message', Buffer.from('{}'))+ await vi.advanceTimersByTimeAsync(40_000)+ expect(ws.terminated).toBe(0)+ ws.emit('ping')+ await vi.advanceTimersByTimeAsync(40_000)+ expect(ws.terminated).toBe(0)+ await vi.advanceTimersByTimeAsync(WS_IDLE_DEADLINE_MS)+ expect(ws.terminated).toBe(1)+ })++ it('runs the piggybacked tick only on a surviving socket, and stops cleanly', async () => {+ vi.useFakeTimers()+ const ws = socket()+ ws.silent = true+ let ticks = 0+ const watch = watchSocketLiveness(ws, { onTick: () => { ticks++ } })+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS * 2)+ expect(ticks).toBe(2)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS)+ expect(ws.terminated).toBe(1)+ expect(ticks).toBe(2) // the terminating tick does no other work+ watch.stop()+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS * 5)+ expect(ws.terminated).toBe(1)+ })++ it('stops by itself when the socket closes, whether or not the owner remembered to', async () => {+ vi.useFakeTimers()+ const ws = socket()+ watchSocketLiveness(ws)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS)+ expect(ws.pings).toBe(1)+ ws.emit('close', 1000)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS * 5)+ expect(ws.pings).toBe(1)+ })++ it('terminates a socket it cannot even ping', async () => {+ vi.useFakeTimers()+ const ws = socket()+ ws.pingThrows = true+ watchSocketLiveness(ws)+ await vi.advanceTimersByTimeAsync(WS_HEARTBEAT_MS)+ expect(ws.terminated).toBe(1)+ })+})diff --git a/cli/src/lib/wsLiveness.ts b/cli/src/lib/wsLiveness.tsnew file mode 100644index 000000000..09c796c65--- /dev/null+++ b/cli/src/lib/wsLiveness.ts@@ -0,0 +1,70 @@+/**+ * Liveness for a WebSocket this daemon holds — a DEADLINE ON SILENCE, not "one missed pong".+ *+ * Three sockets used to carry three copies of the same heartbeat, each pinging every 20s and+ * terminating the moment the previous ping had no pong — and counting pongs only. Two failure modes+ * followed, both measured in harness.log as `close 1006`:+ * - a peer busy streaming data frames whose control-frame pong arrived late lost a working link;+ * - every macOS DarkWake (Power Nap, ~5–10s of wake) fired the timer with the pre-sleep ping still+ * unanswered and terminated on the spot, hundreds of times a week, with no chance to recover.+ *+ * Here ANY inbound frame proves the peer — data, its own ping, a pong — and the socket is only given+ * up after `WS_IDLE_DEADLINE_MS` of nothing at all: three pings' worth, the same shape the backend's+ * `trackSocketLiveness` applies from its side (75s there). Silence is measured on the MONOTONIC clock+ * (`performance.now()` does not advance while the machine sleeps), so the deadline is sixty seconds of+ * silence while awake — a lid closed for an hour is not an hour of silence, and the link gets its+ * three pings after the wake before anyone gives up on it.+ */++import type { WebSocket } from 'ws'++export const WS_HEARTBEAT_MS = 20_000+export const WS_IDLE_DEADLINE_MS = 60_000++export interface LivenessWatch {+ /** Stop pinging and judging. Idempotent; the socket itself is left alone. */+ stop(): void+}++export interface LivenessOptions {+ heartbeatMs?: number+ deadlineMs?: number+ /** Runs on every heartbeat tick the socket survives — for work that rides the same cadence. */+ onTick?: () => void+ /** Announced right before a silent socket is terminated, so a later trace can tell "we gave up"+ * from "the network did": both close as 1006, and only this one is said out loud first. */+ onIdle?: (idleMs: number) => void+}++/** Start watching an OPEN socket. Call once the handshake is done — the clock starts now. */+export function watchSocketLiveness(ws: WebSocket, opts: LivenessOptions = {}): LivenessWatch {+ const deadlineMs = opts.deadlineMs ?? WS_IDLE_DEADLINE_MS+ let lastAliveAt = performance.now()+ const markAlive = (): void => { lastAliveAt = performance.now() }+ ws.on('pong', markAlive)+ ws.on('ping', markAlive) // the peer's own liveness ping — a proof that costs nothing+ ws.on('message', markAlive) // data flowing is the strongest proof there is+ // Judging ends with the verdict: the socket's own 'close' is what the owner cleans up on, and a+ // watch that kept firing after its terminate would only terminate a dead socket again.+ const stop = (): void => { clearInterval(timer) }+ const giveUp = (): void => {+ stop()+ try { ws.terminate() } catch { /* ignore */ }+ }+ // Belt and suspenders for an owner that forgets: `ws.ping()` on a closed socket does not throw+ // (it is silently dropped), so an unstopped watch would tick against a corpse forever.+ ws.once('close', stop)+ const timer = setInterval(() => {+ const idleMs = performance.now() - lastAliveAt+ if (idleMs >= deadlineMs) {+ opts.onIdle?.(idleMs)+ giveUp()+ return+ }+ // A ping that cannot even be written means the socket is already gone under us.+ try { ws.ping() } catch { giveUp(); return }+ opts.onTick?.()+ }, opts.heartbeatMs ?? WS_HEARTBEAT_MS)+ timer.unref?.()+ return { stop }+}diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.tsindex c66a93581..abcb8a5f3 100644--- a/cli/src/localWsServer.ts+++ b/cli/src/localWsServer.ts@@ -3,6 +3,7 @@ import type { AppSwarms } from './cable/cableSession.js'import type http from 'node:http'import type { Socket } from 'node:net'import { WebSocket, WebSocketServer, type RawData } from 'ws'+import { watchSocketLiveness } from './lib/wsLiveness.js'import type { Frame, LocalClientSink } from './backendSocket.js'import {decodeTerminalLocal,@@ -16,12 +17,12 @@ export const LOCAL_WS_PATH = '/api/local-ws'export const LOCAL_WS_PROTOCOL_VERSION = 1const MAX_JSON_BYTES = 512 * 1024+const LOCAL_IDLE_DEADLINE_MS = 40_000// The `ws` library enforces this on EVERY message on this socket, JSON or binary — so it has to// cover the largest binary frame this transport carries, not just JSON control frames. That is a// paste (see TERMINAL_LOCAL_PASTE_MAX_PAYLOAD_BYTES in terminalBinary.ts), plus a little slack for// the local frame header; ordinary JSON frames stay bounded by MAX_JSON_BYTES regardless.const MAX_WS_MESSAGE_BYTES = TERMINAL_LOCAL_PASTE_MAX_PAYLOAD_BYTES + 4_096-const HEARTBEAT_MS = 20_000import type { WindowVoiceReply } from './cable/windowRoute.js'@@ -228,7 +229,6 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO/** Whether this connection ever reported a tile roster — only then is clearing it ours to do. */let sentPanes = falselet sentSwarms = false- let alive = truelet chain = Promise.resolve()const sink: LocalClientSink = {@@ -246,7 +246,6 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerOtry { ws.close(code, reason) } catch { ws.terminate() }}- ws.on('pong', () => { alive = true })ws.on('message', (raw, isBinary) => {chain = chain.then(async () => {if (!selected) {@@ -446,15 +445,16 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO}).catch(() => close(1011, 'local dispatch failed'))})- const heartbeat = setInterval(() => {- if (!alive) { ws.terminate(); return }- alive = false- try { ws.ping() } catch { ws.terminate() }- }, HEARTBEAT_MS)- heartbeat.unref?.()+ // Loopback: a late pong here means the app is hung or gone, not a slow network, so the deadline is+ // tighter than the cloud link's — two pings, not three. Noticing a crashed window sooner is what+ // clears its tile roster (see cleanup) sooner.+ const heartbeat = watchSocketLiveness(ws, {+ deadlineMs: LOCAL_IDLE_DEADLINE_MS,+ onIdle: (idleMs) => console.log(`[local-ws] ${connId} no traffic for ${Math.round(idleMs / 1000)}s — terminating`),+ })const cleanup = (): void => {- clearInterval(heartbeat)+ heartbeat.stop()if (boundMachineId) options.onAppFocusState?.(boundMachineId, null, connId)// A window that went away has no tiles open. Left standing, the roster// would keep silencing the dial for agents nobody can see any more —