Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(presence): stop redundant broadcasts on the presence hot path

enes32ccb09f7cbb
diff --git a/src/core/presence/hub.test.ts b/src/core/presence/hub.test.ts
index 0c49bc570..adac1cc52 100644
--- a/src/core/presence/hub.test.ts
+++ b/src/core/presence/hub.test.ts
@@ -119,6 +119,30 @@ describe('PresenceHub hello', () => {
expect(res).toEqual({ clientId: 99, peers: hub.peers() })
expect(hub.peers()).toHaveLength(1)
})
+
+ // A reconnecting client re-sends its stored identity; that must not fan an identical diff out
+ // to every peer. But it must STILL get its own clientId back — that return is how the renderer
+ // learns which cursor is its own.
+ it('an unchanged identity broadcasts nothing but still returns {clientId, peers}', () => {
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ hub.join(2, 'browser')
+ hub.hello(2, { name: 'Enes', color: PRESENCE_COLORS[3] })
+ const after = diffs().length
+
+ const res = hub.hello(2, { name: 'Enes', color: PRESENCE_COLORS[3] })
+ expect(diffs()).toHaveLength(after) // no change → no diff
+ expect(res.clientId).toBe(2)
+ expect(res.peers.map((p) => p.clientId)).toEqual([1, 2])
+
+ // A real change still broadcasts.
+ hub.hello(2, { name: 'Enes', color: PRESENCE_COLORS[4] })
+ expect(diffs().at(-1)).toEqual({
+ op: 'update',
+ clientId: 2,
+ patch: { name: 'Enes', color: PRESENCE_COLORS[4] }
+ })
+ })
})
describe('PresenceHub signals', () => {
@@ -185,6 +209,83 @@ describe('PresenceHub signals', () => {
// The cap is enforced at ingest, so the broadcast diff carries the capped text too.
expect(diffs().at(-1)).toEqual({ op: 'update', clientId: 1, patch: { chat } })
})
+
+ // The cursor is recomputed (and re-sent) on every pan/zoom frame, and mouse-leave/blur both
+ // send null. An unchanged value must not fan out to every peer for zero visual change.
+ it('setCursor: an unchanged position (and a repeated null) is a silent no-op', () => {
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ hub.setCursor(1, { x: 10, y: 20 })
+ const after = diffs().length
+
+ hub.setCursor(1, { x: 10, y: 20 }) // same position (pan/zoom recompute) → no diff
+ expect(diffs()).toHaveLength(after)
+
+ hub.setCursor(1, { x: 10, y: 21 }) // y moved → broadcast
+ expect(diffs()).toHaveLength(after + 1)
+ hub.setCursor(1, { x: 11, y: 21 }) // x moved → broadcast
+ expect(diffs()).toHaveLength(after + 2)
+
+ hub.setCursor(1, null) // mouse leave → broadcast
+ expect(diffs()).toHaveLength(after + 3)
+ hub.setCursor(1, null) // blur right after leave → already null → no diff
+ expect(diffs()).toHaveLength(after + 3)
+ expect(hub.peers()[0].cursor).toBeNull()
+
+ hub.setCursor(1, { x: 11, y: 21 }) // back from null → broadcast
+ expect(diffs()).toHaveLength(after + 4)
+ })
+
+ // Esc-then-blur closes the bubble twice, and a re-render can re-send identical text.
+ it('setChat: unchanged text (and a repeated null) is a silent no-op, compared AFTER the cap', () => {
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ hub.setChat(1, 'hey')
+ const after = diffs().length
+
+ hub.setChat(1, 'hey') // identical → no diff
+ expect(diffs()).toHaveLength(after)
+ hub.setChat(1, 'hey!') // changed → broadcast
+ expect(diffs()).toHaveLength(after + 1)
+
+ hub.setChat(1, null) // Esc closes the bubble
+ expect(diffs()).toHaveLength(after + 2)
+ hub.setChat(1, null) // blur closes it again → already null → no diff
+ expect(diffs()).toHaveLength(after + 2)
+
+ // Two DIFFERENT over-long strings that cap to the same 200 code points are the same peer
+ // state — comparing the raw text would leak a broadcast per keystroke past the cap.
+ hub.setChat(1, 'z'.repeat(500))
+ const capped = diffs().length
+ hub.setChat(1, 'z'.repeat(600))
+ expect(diffs()).toHaveLength(capped)
+ expect(hub.peers()[0].chat).toBe('z'.repeat(200))
+ })
+
+ it('peers() and the join diff hand out copies, so a caller cannot corrupt hub state', () => {
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ hub.setCursor(1, { x: 1, y: 2 })
+ hub.noteTyping(1, 'node-a')
+
+ // Nested objects are copies too — mutating them must not reach the hub's table.
+ const snap = hub.peers()[0]
+ snap.name = 'hacked'
+ ;(snap.cursor as { x: number; y: number }).x = 999
+ ;(snap.typing as { nodeId: string }).nodeId = 'node-evil'
+ expect(hub.peers()[0]).toMatchObject({
+ name: 'Someone',
+ cursor: { x: 1, y: 2 },
+ typing: { nodeId: 'node-a' }
+ })
+
+ // The join diff carries a copy, not the live PeerState reference.
+ hub.join(2, 'browser')
+ const joined = diffs().at(-1) as { op: 'join'; peer: PeerState }
+ expect(joined.op).toBe('join')
+ hub.setFocus(2, 'node-z')
+ expect(joined.peer.focus).toBeNull() // a live reference would have followed the setter
+ })
})
describe('PresenceHub noteTyping (Stage 2 caller; wired here)', () => {
@@ -208,6 +309,45 @@ describe('PresenceHub noteTyping (Stage 2 caller; wired here)', () => {
hub.noteTyping(1, 'node-a')
expect(diffs()).toHaveLength(before + 3)
})
+
+ // The throttle key is (client, node) ALONE. A throttle that also required the peer's *current*
+ // typing badge to be on the same node would be defeated by a client writing alternately into
+ // two nodes (split terminal, agent + shell): every call would see a different last node and
+ // broadcast, at keystroke rate, to every peer.
+ it('throttles per (client, node) even when the client alternates between two nodes', () => {
+ vi.useFakeTimers()
+ vi.setSystemTime(2_000_000)
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ const before = diffs().length
+
+ // 20 interleaved keystrokes inside one throttle window → one broadcast per node, not 20.
+ for (let i = 0; i < 10; i++) {
+ hub.noteTyping(1, 'node-a')
+ hub.noteTyping(1, 'node-b')
+ }
+ expect(diffs()).toHaveLength(before + 2)
+
+ // Each node's window is its own: node-a's expires, node-b's has not.
+ vi.setSystemTime(2_000_000 + TYPING_THROTTLE_MS + 1)
+ hub.noteTyping(1, 'node-a')
+ expect(diffs()).toHaveLength(before + 3)
+ hub.noteTyping(1, 'node-b')
+ expect(diffs()).toHaveLength(before + 4) // node-b's window opened at the same time → also due
+ })
+
+ // Two clients typing in the same node must not throttle each other (the key carries the client).
+ it('throttles per client, not globally per node', () => {
+ vi.useFakeTimers()
+ vi.setSystemTime(3_000_000)
+ const hub = new PresenceHub()
+ hub.join(1, 'browser')
+ hub.join(2, 'browser')
+ const before = diffs().length
+ hub.noteTyping(1, 'node-a')
+ hub.noteTyping(2, 'node-a')
+ expect(diffs()).toHaveLength(before + 2)
+ })
})
describe('registerIpc', () => {
diff --git a/src/core/presence/hub.ts b/src/core/presence/hub.ts
index 3d9fee66c..211134a7e 100644
--- a/src/core/presence/hub.ts
+++ b/src/core/presence/hub.ts
@@ -34,6 +34,17 @@ export function allocateRelayClientId(): ClientId {
return nextRelayId++
}
+/** A peer copy safe to hand out: the nested `cursor`/`typing` objects are cloned too, so a caller
+ * holding a peers()/join-diff result cannot reach back into the hub's table through them.
+ * (The setters already assign fresh objects, so these two are the only nested state.) */
+function copyPeer(p: PeerState): PeerState {
+ return {
+ ...p,
+ cursor: p.cursor ? { ...p.cursor } : null,
+ typing: p.typing ? { ...p.typing } : null
+ }
+}
+
export class PresenceHub {
private table = new Map<ClientId, PeerState>()
/** `${clientId}:${nodeId}` → last typing broadcast (throttle window). */
@@ -60,7 +71,11 @@ export class PresenceHub {
// The newcomer gets the whole table up front. A relay/phone peer cannot make a request, and a
// browser's ws-bridge buffers events that land before it subscribes — so this is safe for both.
platform().sendTo(clientId, IPC.presenceSync, this.peers())
- this.emit({ op: 'join', peer })
+ // A COPY, never the live PeerState: the diff crosses the shell (and, in-process on the
+ // desktop, is handed to listeners as-is), so broadcasting the table's own object would let a
+ // consumer hold — or mutate — hub state, and would make every later setter retroactively
+ // rewrite an already-delivered diff.
+ this.emit({ op: 'join', peer: copyPeer(peer) })
}
/** A UI disconnected. Its color frees up for the next joiner. */
@@ -78,9 +93,14 @@ export class PresenceHub {
const peer = this.table.get(clientId)
if (!peer) return { clientId, peers: this.peers() }
const { name, color } = sanitizeIdentity(id, { name: peer.name, color: peer.color })
- peer.name = name
- peer.color = color
- this.emit({ op: 'update', clientId, patch: { name, color } })
+ // A reconnecting client re-sends its stored identity: unchanged → no broadcast (same rule as
+ // every setter below). The RETURN is unconditional — it is how the renderer learns its own
+ // ClientId, without which it would draw its own cursor as a peer's.
+ if (peer.name !== name || peer.color !== color) {
+ peer.name = name
+ peer.color = color
+ this.emit({ op: 'update', clientId, patch: { name, color } })
+ }
return { clientId, peers: this.peers() }
}
@@ -93,6 +113,13 @@ export class PresenceHub {
if (!Number.isFinite(cursor.x) || !Number.isFinite(cursor.y)) return
next = { x: cursor.x, y: cursor.y }
}
+ // Unchanged → no broadcast. The renderer recomputes the flow position on every pan/zoom frame
+ // and sends null on both mouse-leave and blur, so identical values are common — and each one
+ // would otherwise fan out to every peer for zero visual change.
+ const prev = peer.cursor
+ if (prev === null ? next === null : next !== null && prev.x === next.x && prev.y === next.y) {
+ return
+ }
peer.cursor = next
this.emit({ op: 'update', clientId, patch: { cursor: next } })
}
@@ -114,6 +141,9 @@ export class PresenceHub {
const peer = this.table.get(clientId)
if (!peer) return
const next = typeof text === 'string' ? capCodePoints(text, CHAT_MAX_LEN) : null
+ // Unchanged → no broadcast (Esc-then-blur closes the bubble twice). Compared AFTER the cap:
+ // two different over-long strings that cap to the same 200 code points are the same peer state.
+ if (peer.chat === next) return
peer.chat = next
this.emit({ op: 'update', clientId, patch: { chat: next } })
}
@@ -138,9 +168,13 @@ export class PresenceHub {
const peer = this.table.get(clientId)
if (!peer || !nodeId) return
const now = Date.now()
+ // The throttle key is (client, node) ALONE — never the peer's *current* typing badge. Someone
+ // writing alternately into two nodes (agent + shell) would never match `peer.typing.nodeId`,
+ // so every keystroke would broadcast to every peer. Each node keeps its own window; a
+ // different node is a different key and so still re-fires immediately.
const key = `${clientId}:${nodeId}`
const last = this.lastTyping.get(key) ?? 0
- if (peer.typing?.nodeId === nodeId && now - last < TYPING_THROTTLE_MS) return
+ if (last && now - last < TYPING_THROTTLE_MS) return
this.lastTyping.set(key, now)
const typing = { nodeId, at: now }
peer.typing = typing
@@ -149,7 +183,7 @@ export class PresenceHub {
/** The current table, in join order. Copies so a caller can't mutate hub state. */
peers(): PeerState[] {
- return [...this.table.values()].map((p) => ({ ...p }))
+ return [...this.table.values()].map(copyPeer)
}
/** Wire the presence channels into the shell. Idempotent on purpose: the shell's listener