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.

feat(dino): wire presence.dino cast + selectDino selector (preload/bridge/session) (dino Task 2)

enes1a3e4c06a979
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 51387b0bd..41ade8332 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -425,6 +425,7 @@ const api: NodeTerminalApi = {
cursor: (cursor) => ipcRenderer.send(IPC.presenceCursor, cursor),
focus: (nodeId) => ipcRenderer.send(IPC.presenceFocus, nodeId),
chat: (text) => ipcRenderer.send(IPC.presenceChat, text),
+ dino: (payload) => ipcRenderer.send(IPC.presenceDino, payload),
project: (projectId) => ipcRenderer.send(IPC.presenceProject, projectId),
onSync: (listener) => {
const handler = (_e: unknown, peers: PeerState[]) => listener(peers)
diff --git a/src/renderer/bridge/ws-bridge.presence.test.ts b/src/renderer/bridge/ws-bridge.presence.test.ts
index 92520354b..0089ff2d1 100644
--- a/src/renderer/bridge/ws-bridge.presence.test.ts
+++ b/src/renderer/bridge/ws-bridge.presence.test.ts
@@ -24,7 +24,7 @@ function fakeClient() {
}
describe('buildPresenceApi', () => {
- it('hello is a request; cursor/focus/chat/project are casts; onSync/onPeer subscribe', async () => {
+ it('hello is a request; cursor/focus/chat/dino/project are casts; onSync/onPeer subscribe', async () => {
const c = fakeClient()
const { presence } = buildPresenceApi(c as never)
@@ -40,12 +40,29 @@ describe('buildPresenceApi', () => {
presence.cursor(null)
presence.focus('node-a')
presence.chat('hey')
+ const dinoPayload = {
+ nodeId: 'dino-a',
+ snap: {
+ y: 0,
+ ducking: false,
+ crashed: false,
+ started: true,
+ score: 1,
+ speed: 6,
+ groundScroll: 0,
+ obstacles: []
+ }
+ }
+ presence.dino(dinoPayload)
+ presence.dino(null)
presence.project('web')
expect(c.casts).toEqual([
{ method: IPC.presenceCursor, args: [{ x: 1, y: 2 }] },
{ method: IPC.presenceCursor, args: [null] },
{ method: IPC.presenceFocus, args: ['node-a'] },
{ method: IPC.presenceChat, args: ['hey'] },
+ { method: IPC.presenceDino, args: [dinoPayload] },
+ { method: IPC.presenceDino, args: [null] },
{ method: IPC.presenceProject, args: ['web'] }
])
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index c1e8e8ced..c792c56eb 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -422,6 +422,7 @@ export function buildPresenceApi(client: RpcClient): Pick<NodeTerminalApi, 'pres
cursor: (cursor) => client.cast(IPC.presenceCursor, cursor),
focus: (nodeId) => client.cast(IPC.presenceFocus, nodeId),
chat: (text) => client.cast(IPC.presenceChat, text),
+ dino: (payload) => client.cast(IPC.presenceDino, payload),
project: (projectId) => client.cast(IPC.presenceProject, projectId),
onSync: (listener) => client.subscribe(IPC.presenceSync, listener as Listener),
onPeer: (listener) => client.subscribe(IPC.presencePeer, listener as Listener)
diff --git a/src/renderer/state/presence.test.ts b/src/renderer/state/presence.test.ts
index 265c63a28..b4d13de11 100644
--- a/src/renderer/state/presence.test.ts
+++ b/src/renderer/state/presence.test.ts
@@ -1,5 +1,20 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
-import { PRESENCE_COLORS, type PeerState } from '@shared/presence'
+import { PRESENCE_COLORS, type DinoSnapshot, type PeerState } from '@shared/presence'
+
+/** A minimal live dino snapshot — the fields are irrelevant to these selector/cast tests. */
+function snap(over: Partial<DinoSnapshot> = {}): DinoSnapshot {
+ return {
+ y: 0,
+ ducking: false,
+ crashed: false,
+ started: true,
+ score: 0,
+ speed: 6,
+ groundScroll: 0,
+ obstacles: [],
+ ...over
+ }
+}
// The store reads localStorage at call time; each test stubs a fresh in-memory storage and
// re-imports the module (vi.resetModules) so nothing leaks between cases.
@@ -55,6 +70,7 @@ function fakePresenceApi(
cursor: (c: unknown) => calls.push(['cursor', c]),
focus: (n: unknown) => calls.push(['focus', n]),
chat: (t: unknown) => calls.push(['chat', t]),
+ dino: (payload: unknown) => calls.push(['dino', payload]),
project: (p: unknown) => calls.push(['project', p]),
onSync: (cb: (peers: PeerState[]) => void) => {
subs.sync++
@@ -550,3 +566,63 @@ describe('selectFocusedFaces (the node-header chips — one per node, so cursor
stop()
})
})
+
+describe('selectDino (the spectator picks THE authority broadcasting for a node)', () => {
+ it('returns the peer broadcasting for this node, excludes self and other node ids, tiebreaks on lowest clientId', async () => {
+ vi.stubGlobal('localStorage', memStorage(STORED_ME))
+ const api = fakePresenceApi({ clientId: 7, peers: [peer(7)] })
+ const { connectPresence, usePresence, selectDino } = await import('./presence')
+ const stop = connectPresence()
+ await vi.waitFor(() => expect(usePresence.getState().myId).toBe(7))
+
+ // Nobody broadcasting yet → null.
+ expect(selectDino(usePresence.getState(), 'dino-a')).toBeNull()
+
+ // A peer becomes the authority for dino-a → we spectate them.
+ api.emitPeer({ op: 'join', peer: peer(8, { dino: { nodeId: 'dino-a', snap: snap() } }) })
+ expect(selectDino(usePresence.getState(), 'dino-a')?.clientId).toBe(8)
+
+ // A peer broadcasting for a DIFFERENT node must not be picked for dino-a.
+ api.emitPeer({ op: 'join', peer: peer(9, { dino: { nodeId: 'dino-b', snap: snap() } }) })
+ expect(selectDino(usePresence.getState(), 'dino-a')?.clientId).toBe(8)
+ expect(selectDino(usePresence.getState(), 'dino-b')?.clientId).toBe(9)
+
+ // Take-over race: two peers broadcast for dino-a at once → lowest clientId wins.
+ api.emitPeer({ op: 'join', peer: peer(5, { dino: { nodeId: 'dino-a', snap: snap() } }) })
+ expect(selectDino(usePresence.getState(), 'dino-a')?.clientId).toBe(5)
+
+ stop()
+ })
+
+ it('never returns my OWN broadcast, even if I am the authority for the node', async () => {
+ vi.stubGlobal('localStorage', memStorage(STORED_ME))
+ // My own peer (id 7) is broadcasting for dino-a in the table — I must not spectate myself.
+ const api = fakePresenceApi({
+ clientId: 7,
+ peers: [peer(7, { dino: { nodeId: 'dino-a', snap: snap() } })]
+ })
+ const { connectPresence, usePresence, selectDino } = await import('./presence')
+ const stop = connectPresence()
+ await vi.waitFor(() => expect(usePresence.getState().myId).toBe(7))
+
+ expect(selectDino(usePresence.getState(), 'dino-a')).toBeNull()
+ stop()
+ })
+})
+
+describe('session.dino (casts the live snapshot on the api)', () => {
+ it('forwards the payload (and the clearing null) straight to api.presence.dino', async () => {
+ vi.stubGlobal('localStorage', memStorage(STORED_ME))
+ const api = fakePresenceApi()
+ const { defaultPresence } = await import('./presence')
+
+ const payload = { nodeId: 'dino-a', snap: snap({ score: 42 }) }
+ defaultPresence.dino(payload)
+ defaultPresence.dino(null)
+
+ expect(api.calls.filter((c) => c[0] === 'dino')).toEqual([
+ ['dino', payload],
+ ['dino', null]
+ ])
+ })
+})
diff --git a/src/renderer/state/presence.ts b/src/renderer/state/presence.ts
index a99876fbf..6d39fb84e 100644
--- a/src/renderer/state/presence.ts
+++ b/src/renderer/state/presence.ts
@@ -4,6 +4,7 @@ import {
nextFreeColor,
peersOnProject,
type ClientId,
+ type DinoSnapshot,
type PeerDiff,
type PeerIdentity,
type PeerState
@@ -138,6 +139,24 @@ export function selectFocused(
return selectVisible(s, projectId).filter((p) => p.focus === nodeId)
}
+/** The OTHER peer (never me — filtered on `myId`, like selectOthers) who is the live dino
+ * authority for `nodeId`: their `dino` is non-null and its `nodeId` matches. If several peers
+ * broadcast for the same node during a brief take-over race, the one with the LOWEST clientId
+ * wins (the authority tiebreak — same rule DinoNode applies), so every spectator converges on
+ * one authority. Returns null when nobody is broadcasting for this node.
+ * While `myId` is null (hello in flight) we cannot tell our own peer from a teammate's, so we
+ * return null — same gate as every other selector here. */
+export function selectDino(s: PresenceStore, nodeId: string): PeerState | null {
+ if (s.myId === null) return null
+ let winner: PeerState | null = null
+ for (const p of Object.values(s.peers)) {
+ if (p.clientId === s.myId) continue
+ if (!p.dino || p.dino.nodeId !== nodeId) continue
+ if (winner === null || p.clientId < winner.clientId) winner = p
+ }
+ return winner
+}
+
/** What the facepile draws — and NOTHING else. `applyDiff` replaces the whole PeerState object on
* every cursor patch (~20/s per peer), so a facepile subscribed to PeerState would re-render at
* cursor rate even under useShallow. selectFaces projects these five fields and reuses the
@@ -186,9 +205,13 @@ export interface PresenceSession {
reportFocus(nodeId: string | null): void
releaseFocus(nodeId: string): void
reportProject(projectId: string | null): void
+ /** Broadcast the live dino game we are the authority for (null = stopped/idle). A pure cast on
+ * the session's api — no dedup: the DinoNode already throttles to ~20 Hz and null-clears. */
+ dino(payload: { nodeId: string; snap: DinoSnapshot } | null): void
selectOthers(s: PresenceStore): PeerState[]
selectVisible(s: PresenceStore, projectId: string | null): PeerState[]
selectFocused(s: PresenceStore, nodeId: string, projectId: string | null): PeerState[]
+ selectDino(s: PresenceStore, nodeId: string): PeerState | null
selectFaces(s: PresenceStore): PeerFace[]
selectFocusedFaces(s: PresenceStore, nodeId: string, projectId: string | null): PeerFace[]
selectTypingFaces(s: PresenceStore, nodeId: string): PeerFace[]
@@ -563,15 +586,24 @@ function buildPresenceSession(api: NodeTerminalApi): PresenceSession {
api.presence.project(projectId)
}
+ /** Broadcast the live dino game we are the authority for (null = stopped/idle). Unlike focus/
+ * project this is NOT deduped: the DinoNode throttles the snapshot rate itself, each frame is
+ * distinct, and the clearing null must always reach the hub so spectators stop drawing. */
+ function dino(payload: { nodeId: string; snap: DinoSnapshot } | null): void {
+ api.presence.dino(payload)
+ }
+
return {
store,
connect,
reportFocus,
releaseFocus,
reportProject,
+ dino,
selectOthers,
selectVisible,
selectFocused,
+ selectDino,
selectFaces,
selectFocusedFaces,
selectTypingFaces
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 996bfbf87..9e8b267c8 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -4,7 +4,7 @@ import type { CloneProgress } from './clone-url'
import type { NormalizedAgentEvent } from './agents/normalize'
import type { AgentId, AgentPermissionMode, PromptInjectionMode } from './agents/config'
import type { GroupWorktree } from './worktree'
-import type { ClientId, PeerDiff, PeerIdentity, PeerState } from './presence'
+import type { ClientId, DinoSnapshot, PeerDiff, PeerIdentity, PeerState } from './presence'
export interface PtyCreateOptions {
shell?: string
@@ -1230,6 +1230,9 @@ export interface PresenceApi {
focus(nodeId: string | null): void
/** Publish live cursor-chat text (null closes the bubble). */
chat(text: string | null): void
+ /** Publish the live dino game we are the authority for (null = stopped/idle). Spectators read
+ * the matching peer's `dino` and render `snap` instead of running their own sim. */
+ dino(payload: { nodeId: string; snap: DinoSnapshot } | null): void
/** Publish the project (canvas) we are looking at — peers on other projects are never drawn
* on our canvas, and we are never drawn on theirs (null = no project open). */
project(projectId: string | null): void