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.

docs(plan): live dino spectator mode — 5 tasks

enesf6c18137688a
diff --git a/docs/superpowers/plans/2026-07-14-dino-live.md b/docs/superpowers/plans/2026-07-14-dino-live.md
new file mode 100644
index 000000000..c2f7fa960
--- /dev/null
+++ b/docs/superpowers/plans/2026-07-14-dino-live.md
@@ -0,0 +1,92 @@
+# Plan: live (shared) dino game — spectator mode
+
+**Branch:** `feat/dino-live` (worktree `/root/nodeterm-dino`, off `main`)
+**Spec:** docs/superpowers/specs/2026-07-14-dino-live-design.md (approved)
+
+## Summary
+
+The dino game state is local-only, so a shared canvas shows independent dinos. Make it a **spectator
+feature**: one authority plays, others watch the same game live — by riding the presence layer (an
+ephemeral `PeerState.dino` field, like `chat`). No new core channel; works over local/relay/browser
+via the hub automatically.
+
+## Tasks (subagent-driven, TDD, review each) — sequential (one worktree)
+
+### Task 1 — Shared types + hub cast handler (foundation)
+- `src/shared/presence.ts`: add `DinoSnapshot` (y, ducking, crashed, started, score, speed,
+ groundScroll, obstacles[{kind,x,y,sx,sw,sh,flap}]); add `dino: { nodeId: string; snap: DinoSnapshot } | null`
+ to `PeerState`; add caps `DINO_MAX_OBSTACLES` (≈ 12) and a `sanitizeDinoPayload(payload): {nodeId,snap}|null`
+ pure helper that clamps obstacle count, coerces numbers, caps the nodeId (`REF_MAX_LEN`), and returns
+ null for anything malformed/oversized (mirror the chat/focus cap discipline).
+- `src/core/presence/hub.ts`: handle a new `presence:dino` cast — run `sanitizeDinoPayload`, set the
+ sender's `PeerState.dino`, broadcast a `PeerDiff` `update`. Mirror the existing `presence:chat`
+ handler exactly (same dumb-reflector path). `leave`/disconnect already drops the whole peer state,
+ so `dino` clears for free — confirm no extra work.
+- TDD (`hub.test.ts`): a `dino` cast sets the sender's `dino` + broadcasts an `update`; a payload with
+ > DINO_MAX_OBSTACLES is clamped; a malformed payload → null (dropped); a `leave` clears it.
+- Also add `IPC.presenceDino = 'presence:dino'` to `src/shared/ipc.ts` (a cast, next to presenceChat)
+ so Task 2 wires it — but the HANDLER registration in the shells is Task 2's job if it lives outside
+ the hub; if the hub is where casts are dispatched, register here. Check how `presence:chat` is
+ dispatched (main index / server ws / the hub) and follow that seam.
+
+### Task 2 — Wire: preload + bridge + presence session
+- `src/preload/index.ts`: `presence.dino: (payload) => ipcRenderer.send(IPC.presenceDino, payload)`
+ (mirror `chat`). Update the `PresenceApi` type in `shared/types.ts`.
+- `src/renderer/bridge/*` (ws-bridge / stubs): the browser bridge shim for `presence.dino` (a cast
+ over the WS-RPC, like `presence.cursor`/`chat`). The `satisfies NodeTerminalApi` gate forces the
+ declaration — wire it as a real cast (presence works in the browser), not a stub.
+- `src/renderer/state/presence.ts` `PresenceSession`: add a `dino(payload)` write (casts on the
+ session's `api.presence.dino`) and a `selectDino(state, nodeId)` selector returning the OTHER peer
+ (not self, filtered by `myId`) broadcasting a `dino` whose `nodeId` matches — mirror `selectVisible`
+ / the chat selectors. Session-aware for free (it's on the PresenceSession).
+- TDD (`state/presence.test.ts`): `selectDino(nodeId)` returns a peer broadcasting for that node,
+ excludes self and other node ids; `dino(payload)` casts on the api.
+
+### Task 3 — dino-game engine: broadcast + spectate
+- `src/renderer/nodes/dino/dino-game.ts`: add to `createDinoGame`'s opts an `onSnapshot?: (snap: DinoSnapshot | null) => void`
+ and to its returned interface `setRemote(snap: DinoSnapshot | null): void` + `isAuthority(): boolean`.
+ - **Broadcast:** in `frame()`, at `DINO_BROADCAST_HZ = 20` (throttle by wall-time since last emit),
+ call `onSnapshot(currentSnapshot())` while the local sim is authoritative and `started || crashed`;
+ call `onSnapshot(null)` when it returns to pre-start idle / on `stop()`/`destroy()`.
+ - **Spectate:** `setRemote(snap)` with a non-null snap puts the game in REMOTE mode — the rAF loop
+ still runs (to draw) but `update()` is suspended and input (jump/duck) is ignored; `draw()` renders
+ the remote `snap` (dino y/frame, obstacles, score, GAME OVER). `setRemote(null)` returns to LOCAL
+ mode (own sim resumes from idle). Local input while in REMOTE mode → immediately switch to LOCAL
+ (take-over: seed the sim from the last remote snap for continuity), and the caller starts broadcasting.
+- Keep the file self-contained (no imports beyond the DinoSnapshot type). Extract `currentSnapshot()`
+ and the remote-draw as small internal functions.
+- TDD (`dino/dino-game.test.ts`): drive the state machine without a real canvas where possible (mock
+ the 2d context) — `setRemote(snap)` suspends update + renders the snap; a local key in remote mode
+ switches to local + fires the take-over; `onSnapshot` throttles to ~20 Hz and yields null on idle.
+ If a full canvas mock is too heavy, extract the mode/throttle/snapshot logic into a tiny pure
+ module and test THAT; state what you did.
+
+### Task 4 — DinoNode wiring (authority + spectate + label + tiebreak)
+- `src/renderer/nodes/DinoNode.tsx`: it's under the active-session provider.
+ - Get the active presence: `const presence = useActiveSessionPresence()` and `const { api } = useSession()`.
+ - Pass `onSnapshot: (snap) => api.presence.dino(snap ? { nodeId: id, snap } : null)` to `createDinoGame`.
+ - Subscribe `presence.store(useShallow((s) => selectDino(s, id)))` → the peer (if any) broadcasting
+ for THIS node. When present AND that peer should win the authority tiebreak (**lower clientId wins**;
+ if OUR clientId is lower we keep local play and ignore the remote), call `game.setRemote(peer.dino.snap)`
+ on each new snapshot; when absent, `game.setRemote(null)`.
+ - Render a small "▷ <name> is playing" label (peer color) while spectating.
+ - On unmount, `api.presence.dino(null)` if we were the authority (belt-and-suspenders; the engine's
+ `destroy()`/`stop()` already emits null via onSnapshot).
+- TDD: extract any non-trivial decision (the tiebreak: given my clientId + the broadcasting peer, do I
+ spectate or keep playing?) into a tiny pure helper + unit-test it. The JSX wiring relies on typecheck
+ (no jsdom for DinoNode).
+
+### Task 5 — Gate + docs + acceptance
+- `npm run typecheck` + full `npx vitest run` green (the pre-existing workspace-watcher fs-timing
+ failure is not ours — confirm it's the only red).
+- `docs/team-presence.md` (or remote-sessions.md): note the dino live/spectator feature rides presence.
+- Update the dino spec's status to landed.
+- Two-instance acceptance (append to the checklist): A plays a shared dino → B watches it live with
+ A's name; B presses Space → takeover; A closes/disconnects → B falls back to idle.
+
+## Merge gate
+No crypto/handshake file touched. Full suite + typecheck green. Whole-branch review: the hub cap can't
+be bypassed (a peer can't flood via `presence:dino`); the perf contract holds (the ~20 Hz snapshot
+does not re-render Canvas — DinoNode already re-renders on its own state, and the broadcast is a cast,
+not a Canvas subscription); local-only + solo behavior byte-identical (no peers → cast is a no-op,
+own sim runs as today). Then merge to main.