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(team): renderer store for live/pending Team Access seats (team-access Task 3)

enesb6ce96f31525
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index d001dbc59..90b9257b5 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -101,6 +101,7 @@ import { opensInEditor } from '../lib/openTarget'
import { newEntryPath, parentDir } from '../lib/explorerCreate'
import { useProjects } from '../state/projects'
import { useAgentStatus } from '../state/agentStatus'
+import { useTeamAccessEvents } from '../state/teamAccess'
import { useChatSessions } from '../state/chatSessions'
import { useAgentNodes } from '../state/agentNodes'
import { SubagentNode } from '../nodes/SubagentNode'
@@ -1295,6 +1296,10 @@ export function Canvas() {
return window.nodeTerminal.relayHost.onPeerPending((info) => setPendingPeer(info))
}, [])
+ // Team Access seat table (docs/…/team-access, Task 3): a SEPARATE relay-host subscription set from
+ // the SAS-approval effect above — one feeds the dialog, this one feeds the live/pending seats store.
+ useTeamAccessEvents()
+
// ---- canvas sync (team) ----
// Emitting side: diff each settled node snapshot against the last one we published and cast the
// mutations on `canvas:mut`. The core reflector (src/core/canvas-sync.ts) fans each one out to
diff --git a/src/renderer/state/teamAccess.test.ts b/src/renderer/state/teamAccess.test.ts
new file mode 100644
index 000000000..a63e0042e
--- /dev/null
+++ b/src/renderer/state/teamAccess.test.ts
@@ -0,0 +1,118 @@
+import { beforeEach, describe, expect, it } from 'vitest'
+import {
+ addPending,
+ clearSeats,
+ connectedCount,
+ listSeats,
+ markConnected,
+ markPending,
+ pendingCount,
+ removeSeat,
+ usedCount,
+ type SeatsState
+} from './teamAccessCore'
+import { useTeamAccess } from './teamAccess'
+
+describe('teamAccessCore (pure reducer)', () => {
+ const empty: SeatsState = {}
+
+ it('addPending adds a pending seat with its email', () => {
+ const s = addPending(empty, 'a', '[email protected]')
+ expect(s.a).toEqual({ id: 'a', email: '[email protected]', status: 'pending' })
+ expect(pendingCount(s)).toBe(1)
+ expect(connectedCount(s)).toBe(0)
+ })
+
+ it('markConnected transitions a known pending seat to connected', () => {
+ let s = addPending(empty, 'a', '[email protected]')
+ s = markConnected(s, 'a')
+ expect(s.a.status).toBe('connected')
+ expect(s.a.email).toBe('[email protected]')
+ expect(connectedCount(s)).toBe(1)
+ })
+
+ it('usedCount counts pending AND connected (matches the Task-2 seat cap = byId.size)', () => {
+ let s = addPending(empty, 'a', '[email protected]')
+ s = addPending(s, 'b')
+ // both pending → both consume a seat
+ expect(usedCount(s)).toBe(2)
+ s = markConnected(s, 'a')
+ expect(usedCount(s)).toBe(2)
+ expect(connectedCount(s)).toBe(1)
+ expect(pendingCount(s)).toBe(1)
+ })
+
+ it('remove drops the seat', () => {
+ let s = addPending(empty, 'a', '[email protected]')
+ s = removeSeat(s, 'a')
+ expect(s.a).toBeUndefined()
+ expect(usedCount(s)).toBe(0)
+ })
+
+ it('markPending on an already-added id enriches email without duplicating or losing status', () => {
+ let s = addPending(empty, 'a') // minted without an email
+ s = markPending(s, 'a', '[email protected]') // the peer-pending event carries it
+ expect(Object.keys(s)).toHaveLength(1)
+ expect(s.a.email).toBe('[email protected]')
+ expect(s.a.status).toBe('pending')
+ })
+
+ it('markPending adds the seat when the event beats the invite-return add', () => {
+ const s = markPending(empty, 'a', '[email protected]')
+ expect(s.a).toEqual({ id: 'a', email: '[email protected]', status: 'pending' })
+ })
+
+ it('markPending never downgrades an already-connected seat', () => {
+ let s = addPending(empty, 'a', '[email protected]')
+ s = markConnected(s, 'a')
+ s = markPending(s, 'a')
+ expect(s.a.status).toBe('connected')
+ })
+
+ it('markConnected on an unknown id is a no-op (a seat always exists from mint)', () => {
+ const s = markConnected(empty, 'ghost')
+ expect(s).toEqual({})
+ })
+
+ it('clearSeats empties the record', () => {
+ let s = addPending(empty, 'a', '[email protected]')
+ s = addPending(s, 'b')
+ expect(clearSeats()).toEqual({})
+ })
+
+ it('listSeats returns seats in insertion order', () => {
+ let s = addPending(empty, 'a')
+ s = addPending(s, 'b')
+ s = addPending(s, 'c')
+ expect(listSeats(s).map((e) => e.id)).toEqual(['a', 'b', 'c'])
+ })
+})
+
+describe('useTeamAccess store', () => {
+ beforeEach(() => useTeamAccess.getState().clear())
+
+ it('addPending → markConnected → remove flows through the store', () => {
+ const { addPending: add, markConnected: connect, remove } = useTeamAccess.getState()
+ add('a', '[email protected]')
+ expect(useTeamAccess.getState().seats.a.status).toBe('pending')
+ connect('a')
+ expect(useTeamAccess.getState().seats.a.status).toBe('connected')
+ expect(usedCount(useTeamAccess.getState().seats)).toBe(1)
+ remove('a')
+ expect(useTeamAccess.getState().seats.a).toBeUndefined()
+ })
+
+ it('markPending enriches without duplicating', () => {
+ const st = useTeamAccess.getState()
+ st.addPending('a')
+ st.markPending('a', '[email protected]')
+ expect(Object.keys(useTeamAccess.getState().seats)).toHaveLength(1)
+ expect(useTeamAccess.getState().seats.a.email).toBe('[email protected]')
+ })
+
+ it('clear empties the store', () => {
+ useTeamAccess.getState().addPending('a')
+ useTeamAccess.getState().clear()
+ expect(useTeamAccess.getState().seats).toEqual({})
+ })
+})
diff --git a/src/renderer/state/teamAccess.ts b/src/renderer/state/teamAccess.ts
new file mode 100644
index 000000000..89c60cd50
--- /dev/null
+++ b/src/renderer/state/teamAccess.ts
@@ -0,0 +1,70 @@
+import { useEffect } from 'react'
+import { create } from 'zustand'
+import {
+ addPending as addPendingCore,
+ clearSeats,
+ markConnected as markConnectedCore,
+ markPending as markPendingCore,
+ removeSeat,
+ type SeatsState
+} from './teamAccessCore'
+
+/**
+ * Transient Team Access seat table for the LOCAL host (docs/superpowers/plans/2026-07-15-team-access.md,
+ * Task 3). Hosting is always done from this machine's own core, so — unlike presence/agentStatus —
+ * there is no per-core session machinery here: one module-level store is correct.
+ *
+ * Fed by the relay-host events (Task 2): `relay:host:peer-pending` → `markPending`,
+ * `relay:host:open` → `markConnected`, `relay:host:closed` → `remove`, host stop → `clear`. Task 4's
+ * settings section additionally calls `addPending(id, email)` the instant `relayHost.invite()`
+ * resolves, so the pending row shows before the peer connects.
+ *
+ * PRESENCE JOIN DEFERRED (v1): a seat is keyed by its renderer UUID (`id`), while a presence peer is
+ * keyed by a ClientId — the two id spaces are not trivially joinable, so a seat row shows the invite
+ * `email` label + a generic connected/pending state, NOT the peer's presence name/color. Wiring the
+ * name/color join is a documented follow-up (it needs a seat-id ⇄ ClientId correlation the relay
+ * layer does not currently carry). See the plan's Task 3 note.
+ */
+export interface TeamAccessStore {
+ seats: SeatsState
+ /** A seat minted by `relayHost.invite()` (reserved before the peer connects). */
+ addPending(id: string, email?: string): void
+ /** Ensure/enrich from a `relay:host:peer-pending` event (may beat or follow `addPending`). */
+ markPending(id: string, email?: string): void
+ /** A `relay:host:open` event — flip the known seat to connected. */
+ markConnected(id: string): void
+ /** A `relay:host:closed` event, or a manual revoke — drop the seat. */
+ remove(id: string): void
+ /** Host stop — empty the table. */
+ clear(): void
+}
+
+export const useTeamAccess = create<TeamAccessStore>((set) => ({
+ seats: {},
+ addPending: (id, email) => set((s) => ({ seats: addPendingCore(s.seats, id, email) })),
+ markPending: (id, email) => set((s) => ({ seats: markPendingCore(s.seats, id, email) })),
+ markConnected: (id) => set((s) => ({ seats: markConnectedCore(s.seats, id) })),
+ remove: (id) => set((s) => ({ seats: removeSeat(s.seats, id) })),
+ clear: () => set({ seats: clearSeats() })
+}))
+
+/**
+ * Subscribe the seat store to the relay-host events. Mounted ONCE (from Canvas), this is a SEPARATE
+ * subscription set from Canvas's existing `relayHost.onPeerPending` (which drives the SAS approval
+ * dialog) — both may observe `peer-pending`, and that is fine: one feeds the dialog, one feeds this
+ * store. Do not fold them together. Cleaned up on unmount.
+ */
+export function useTeamAccessEvents(): void {
+ const store = useTeamAccess
+ useEffect(() => {
+ const { markPending, markConnected, remove } = store.getState()
+ const unPending = window.nodeTerminal.relayHost.onPeerPending((e) => markPending(e.id, e.email))
+ const unOpen = window.nodeTerminal.relayHost.onOpen((e) => markConnected(e.id))
+ const unClosed = window.nodeTerminal.relayHost.onClosed((e) => remove(e.id))
+ return () => {
+ unPending()
+ unOpen()
+ unClosed()
+ }
+ }, [store])
+}
diff --git a/src/renderer/state/teamAccessCore.ts b/src/renderer/state/teamAccessCore.ts
new file mode 100644
index 000000000..b75817677
--- /dev/null
+++ b/src/renderer/state/teamAccessCore.ts
@@ -0,0 +1,82 @@
+/**
+ * Pure reducer over the Team Access seat table (docs/superpowers/plans/2026-07-15-team-access.md,
+ * Task 3). One entry per seat the local host has minted for a device — `pending` from the moment
+ * `relayHost.invite()` returns its id (before the peer connects), `connected` once the peer opens.
+ * Transient: nothing here is persisted; the host rebuilds the table from the relay-host events on
+ * the next hosting session.
+ *
+ * COUNTER SEMANTICS — `usedCount` = pending + connected (i.e. the whole table). This is deliberate
+ * and matches the Task-2 seat cap, which counts `byId.size` (every minted seat, reserved or live).
+ * A pending invite RESERVES a seat, so the settings "Used X / N" must count it too — otherwise the
+ * UI would show "Used 1/3" while the host is refused a 3rd invite because two are pending-reserved.
+ * `connectedCount` is exposed separately for anywhere that wants only the live devices.
+ */
+export interface SeatEntry {
+ id: string
+ /** The invitee email this seat was invited with (DISPLAY label only — the SAS is the trust gate). */
+ email?: string
+ status: 'pending' | 'connected'
+}
+
+export type SeatsState = Record<string, SeatEntry>
+
+/** Add a freshly-minted (reserved) seat — called when `relayHost.invite()` resolves, before the
+ * peer connects. Idempotent: a repeat add for an existing id enriches its email (fills a missing
+ * one) and keeps its current status, so it never downgrades a seat that has already connected. */
+export function addPending(seats: SeatsState, id: string, email?: string): SeatsState {
+ const prev = seats[id]
+ if (prev) {
+ return { ...seats, [id]: { ...prev, email: email ?? prev.email } }
+ }
+ return { ...seats, [id]: { id, email, status: 'pending' } }
+}
+
+/** Ensure/enrich a seat from a `relay:host:peer-pending` event (may beat OR follow the invite-return
+ * add). Adds the seat as `pending` if unseen; otherwise enriches its email and leaves its status
+ * alone — so a seat already `connected` is never downgraded by a late/duplicate pending event. */
+export function markPending(seats: SeatsState, id: string, email?: string): SeatsState {
+ return addPending(seats, id, email)
+}
+
+/** Flip a KNOWN seat to `connected` (a `relay:host:open` event). No-op on an unknown id: a seat
+ * always exists from mint (`addPending`), so an open for an id we never minted is ignored rather
+ * than fabricating a seat with no invite context. */
+export function markConnected(seats: SeatsState, id: string): SeatsState {
+ const prev = seats[id]
+ if (!prev) return seats
+ if (prev.status === 'connected') return seats
+ return { ...seats, [id]: { ...prev, status: 'connected' } }
+}
+
+/** Drop a seat (a `relay:host:closed` event, or a manual revoke). */
+export function removeSeat(seats: SeatsState, id: string): SeatsState {
+ if (!(id in seats)) return seats
+ const next = { ...seats }
+ delete next[id]
+ return next
+}
+
+/** Empty the table (host stop). */
+export function clearSeats(): SeatsState {
+ return {}
+}
+
+/** Seats consuming a licensed seat = pending (reserved) + connected — see COUNTER SEMANTICS above. */
+export function usedCount(seats: SeatsState): number {
+ return Object.keys(seats).length
+}
+
+/** Live devices only. */
+export function connectedCount(seats: SeatsState): number {
+ return Object.values(seats).filter((s) => s.status === 'connected').length
+}
+
+/** Reserved-but-not-yet-connected seats. */
+export function pendingCount(seats: SeatsState): number {
+ return Object.values(seats).filter((s) => s.status === 'pending').length
+}
+
+/** The seats as an array in insertion (mint) order. */
+export function listSeats(seats: SeatsState): SeatEntry[] {
+ return Object.values(seats)
+}