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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(relay): reserve seats at mint with a revocable id — closes the cap race + pending-seat leak (team-access Task 2 review)
enes826d873272fc
diff --git a/src/main/remote/relay-host-service.test.ts b/src/main/remote/relay-host-service.test.tsindex 63410e411..220a0e0b0 100644--- a/src/main/remote/relay-host-service.test.ts+++ b/src/main/remote/relay-host-service.test.ts@@ -395,7 +395,7 @@ function makeFakeSession(n: number): FakeSession {/** Wire an `initRelayHost` whose `connect` returns controllable fake sessions, capped at `seats`. */function wirePool(seats: number): {- invite: (opts?: { projectId?: string; email?: string }) => Promise<{ offer: string }>+ invite: (opts?: { projectId?: string; email?: string }) => Promise<{ offer: string; id: string }>captured: Array<{ opts: any; session: FakeSession }>} {const win = fakeWin()@@ -414,7 +414,8 @@ function wirePool(seats: number): {}})return {- invite: (opts = {}) => h.handlers[IPC.relayHostInvite]({}, opts) as Promise<{ offer: string }>,+ invite: (opts = {}) =>+ h.handlers[IPC.relayHostInvite]({}, opts) as Promise<{ offer: string; id: string }>,captured}}@@ -457,6 +458,94 @@ describe('initRelayHost — Team Access pool (invite/cap)', () => {})})+describe('initRelayHost — Task-2 review: reserve-at-mint with a revocable id', () => {+ // Finding 1 — the seat is RESERVED synchronously (before the token-mint await), so two concurrent+ // invites can't both pass the cap. Fire both without awaiting the first, then settle.+ it('closes the seat-cap race: two concurrent invites at cap 1 → exactly one succeeds, one E_SEATS_FULL', async () => {+ const host = wirePool(1)+ const results = await Promise.allSettled([host.invite(), host.invite()])+ const statuses = results.map((r) => r.status)+ expect(statuses.filter((s) => s === 'fulfilled').length).toBe(1)+ const rejected = results.find((r) => r.status === 'rejected') as PromiseRejectedResult | undefined+ expect(String(rejected?.reason)).toContain(E_SEATS_FULL)+ })++ it('invite resolves with a seat id (the same id its peer-pending event later carries)', async () => {+ const host = wirePool(3)+ const { id } = await host.invite({ email: '[email protected]' })+ expect(id).toBeTruthy()+ host.captured[0].opts.onPeerPending(host.captured[0].session)+ expect(openedIds(IPC.relayHostPeerPending).at(-1)?.id).toBe(id)+ })++ // Finding 2 — a minted invite whose peer never connects is revocable by its returned id (no ghost+ // seat). Before this fix `revoke` couldn't reach it (no id until onPeerPending) and only stop() freed+ // it. A reserved-but-never-handshaked session reports no peer key yet, so revoke has nothing live to+ // cut — it just frees the reservation and tells the renderer. Wire a null-peer-key session to model it.+ it('a pending-never-connected seat is revocable by its returned id and frees the seat', async () => {+ vi.mocked(killRelayHostsByPeerKey).mockClear()+ const win = fakeWin()+ const invite = (): Promise<{ offer: string; id: string }> =>+ h.handlers[IPC.relayHostInvite]({}, {}) as Promise<{ offer: string; id: string }>+ initRelayHost(win as never, platform, {+ loadKeys: async () => genKeyPair(),+ mintToken: async () => ({ pairingToken: 'tok' }),+ isPremium: () => true,+ relayAllowed: () => true,+ getEntitlement: () => 'ent',+ licensedSeats: () => 1,+ // A reserved seat whose peer never completes the handshake: no SAS, no peer key.+ connect: () =>+ ({+ clientId: () => null,+ sas: () => null,+ peerKeyB64: () => null,+ sharedProjectId: () => undefined,+ confirm: vi.fn(),+ close: vi.fn()+ }) as unknown as RelayHostSession+ })+ const { id } = await invite()+ // The peer never connects — no onPeerPending fired — yet the cap is already taken.+ await expect(invite()).rejects.toThrow(E_SEATS_FULL)++ h.handlers[IPC.relayHostRevoke]({}, { id })+ // No live socket to cut (the session never opened), but the seat frees and the renderer is told.+ expect(killRelayHostsByPeerKey).not.toHaveBeenCalled()+ expect(openedIds(IPC.relayHostClosed).at(-1)).toEqual({ id })++ // The freed seat is reusable: a fresh invite succeeds.+ await expect(invite()).resolves.toHaveProperty('offer')+ })++ // Rollback — a mint failure AFTER the synchronous reservation must not leak a seat.+ it('rolls the reservation back on a mint failure (no seat leak)', async () => {+ const win = fakeWin()+ let fail = true+ const captured: FakeSession[] = []+ initRelayHost(win as never, platform, {+ loadKeys: async () => genKeyPair(),+ mintToken: async () => {+ if (fail) throw new Error('mint boom')+ return { pairingToken: 'tok' }+ },+ isPremium: () => true,+ relayAllowed: () => true,+ getEntitlement: () => 'ent',+ licensedSeats: () => 1,+ connect: () => {+ const s = makeFakeSession(captured.length)+ captured.push(s)+ return s as unknown as RelayHostSession+ }+ })+ await expect(h.handlers[IPC.relayHostInvite]({}, {})).rejects.toThrow('mint boom')+ // The reservation was rolled back — at cap 1 a fresh (now-succeeding) invite still fits.+ fail = false+ await expect(h.handlers[IPC.relayHostInvite]({}, {})).resolves.toHaveProperty('offer')+ })+})+describe('initRelayHost — Team Access email label rides the events', () => {it('email is carried on peer-pending and open', async () => {const host = wirePool(3)diff --git a/src/main/remote/relay-host-service.ts b/src/main/remote/relay-host-service.tsindex 786d90773..bb71c8b43 100644--- a/src/main/remote/relay-host-service.ts+++ b/src/main/remote/relay-host-service.ts@@ -92,29 +92,28 @@ export function initRelayHost(const getEntitlement = deps.getEntitlement ?? getStoredEntitlementconst licensedSeats = deps.licensedSeats ?? licenseSeats- // Renderer-facing token → the pending/live session it names. Populated once the peer is pending- // (its SAS is known); the confirm/revoke/close paths resolve the session through it so a stray IPC- // event can only ever act on the session it addresses. Mirrors initRemoteHost's `pendingApprovalId`.- const byId = new Map<string, RelayHostSession>()- // The POOL of minted listeners (Team Access): a session JOINS at mint and LEAVES on close. Its size- // is the live+pending seat count the cap compares against `licensedSeats()`. The value holds the- // invite `email` label (display only) that rides this seat's peer-pending/open events. Replaces the- // old single `current` slot — a pool of N is the whole point of Team Access.- const pool = new Map<RelayHostSession, { email?: string }>()+ // The POOL, keyed by renderer id (Team Access). One entry per SEAT — inserted SYNCHRONOUSLY when the+ // seat is minted (reserve-before-await; see addSeat) and removed on close/revoke/stop. `byId.size` is+ // the reserved+pending+live seat count the cap compares against `licensedSeats()`. `session` is+ // `null` while the seat is reserved but its listener has not been wired yet (the brief window across+ // the token mint), then the live `RelayHostSession`; `email` is the invite label (display only) that+ // rides this seat's peer-pending/open events. The renderer id is the SAME token from mint through+ // peer-pending/open/close and revoke, so a stray IPC event can only ever act on the seat it names,+ // and a pending-never-connected seat is revocable from the moment it is minted. Replaces the old+ // dual `byId`/`pool` bookkeeping — one map keyed by the id the UI and revoke already use.+ const byId = new Map<string, { session: RelayHostSession | null; email?: string }>()function send(channel: string, ...args: unknown[]): void {if (!win.isDestroyed()) win.webContents.send(channel, ...args)}- function forget(session: RelayHostSession): void {- for (const [id, s] of byId) if (s === session) byId.delete(id)- }-/*** Mint ONE pairing + open ONE relay host listener, ADDING it to the pool (no supersede). Cap-checked- * against the licensed seats. Returns the offer to hand to the invited device.+ * against the licensed seats. The seat is RESERVED synchronously (with its revocable id) before any+ * await, then filled in once the listener is wired. Returns the offer to hand to the invited device+ * plus the seat's renderer `id`.*/- async function addSeat({ projectId, email }: AddSeatOptions): Promise<{ offer: string }> {+ async function addSeat({ projectId, email }: AddSeatOptions): Promise<{ offer: string; id: string }> {if (!isPremium()) {throw new Error('Remote access requires nodeterm Pro.')}@@ -126,111 +125,127 @@ export function initRelayHost(throw new Error('No entitlement found — please re-activate nodeterm Pro.')}- // SEAT CAP — count minted (pending + live) sessions in the pool. This is HOST-SIDE / UX- // enforcement, NOT a server-guaranteed limit: a host that patched it out only cheats itself (it- // is paying for the seats). Real, un-bypassable enforcement is v2, server-side (the relay refuses- // the (seats+1)th bridge per account). Do NOT close others here — Team Access is ADDITIVE.- if (!canAcceptSeat(pool.size, licensedSeats())) {+ // SEAT CAP + RESERVATION — atomic (no await between reading the count and reserving). The count is+ // minted (reserved + pending + live) seats. This is HOST-SIDE / UX enforcement, NOT a+ // server-guaranteed limit: a host that patched it out only cheats itself (it is paying for the+ // seats). Real, un-bypassable enforcement is v2, server-side (the relay refuses the (seats+1)th+ // bridge per account). Do NOT close others here — Team Access is ADDITIVE.+ if (!canAcceptSeat(byId.size, licensedSeats())) {throw new Error(E_SEATS_FULL)}+ // Reserve the seat SYNCHRONOUSLY — with a revocable id — before any await, so two concurrent+ // invites can't both pass the cap across the token-mint latency (Finding 1), and so a+ // pending-never-connected seat is revocable from the moment it's minted (Finding 2). `session` is+ // filled in once `connect()` wires the listener below.+ const rendererId = randomUUID()+ byId.set(rendererId, { session: null, email })- // A locked keyring surfaces here as a rejected invite (PeerKeyLockedError / E_PEER_KEY_LOCKED):- // the identity on disk is intact and must not be rotated — the renderer tells the human to unlock.- const keys = await loadKeys()- const { pairingToken } = await mintToken(entitlement)-- // The renderer token for THIS listener's eventual peer. Minted when the peer becomes pending, so- // onOpen/onClose reference the same id the peer-pending event carried.- let rendererId: string | null = null-- const session = connect({- url,- token: pairingToken,- ourKeys: keys,- platform,- transport: deps.transport,- // The single project this hosting session shares with the peer. Absent → unscoped, as before.- sharedProjectId: projectId,- // The SAS is known — ask the human to compare it. NOTHING is served yet. The invite `email`- // tags this seat in the renderer (display only).- onPeerPending: (s) => {- rendererId = randomUUID()- byId.set(rendererId, s)- send(IPC.relayHostPeerPending, {- id: rendererId,- sas: s.sas(),- peerKeyB64: s.peerKeyB64() ?? '',- })- },- // Both humans confirmed: the peer is a live CorePlatform client now.- onOpen: () => {- if (rendererId) send(IPC.relayHostOpen, { id: rendererId, email })- },- // The relay socket dropped (the peer is already torn down when this fires). Free the seat.- onClose: () => {- forget(session)- pool.delete(session)- if (rendererId) send(IPC.relayHostClosed, { id: rendererId })- }- })- // Join the pool immediately (before any peer connects) so the cap counts this seat from mint.- pool.set(session, { email })-- return {- offer: encodeOffer({- relayEndpoint: url,- pairingToken,- hostPublicKeyB64: publicKeyToB64(keys.publicKey)+ try {+ // A locked keyring surfaces here as a rejected invite (PeerKeyLockedError / E_PEER_KEY_LOCKED):+ // the identity on disk is intact and must not be rotated — the renderer tells the human to unlock.+ const keys = await loadKeys()+ const { pairingToken } = await mintToken(entitlement)++ const session = connect({+ url,+ token: pairingToken,+ ourKeys: keys,+ platform,+ transport: deps.transport,+ // The single project this hosting session shares with the peer. Absent → unscoped, as before.+ sharedProjectId: projectId,+ // The SAS is known — ask the human to compare it. NOTHING is served yet. Reuse the reserved+ // id (do NOT mint a fresh one) so onOpen/onClose/revoke all name the same seat.+ onPeerPending: (s) => {+ const entry = byId.get(rendererId)+ if (entry) entry.session = s+ send(IPC.relayHostPeerPending, {+ id: rendererId,+ sas: s.sas(),+ peerKeyB64: s.peerKeyB64() ?? '',+ })+ },+ // Both humans confirmed: the peer is a live CorePlatform client now.+ onOpen: () => {+ send(IPC.relayHostOpen, { id: rendererId, email })+ },+ // The relay socket dropped (the peer is already torn down when this fires). Free the seat.+ onClose: () => {+ byId.delete(rendererId)+ send(IPC.relayHostClosed, { id: rendererId })+ }})++ // Fill the reserved slot with the live session. If it was revoked during the await window the+ // entry is gone — don't leak a live listener; close it.+ const entry = byId.get(rendererId)+ if (entry) entry.session = session+ else session.close()++ return {+ offer: encodeOffer({+ relayEndpoint: url,+ pairingToken,+ hostPublicKeyB64: publicKeyToB64(keys.publicKey)+ }),+ id: rendererId+ }+ } catch (err) {+ // Mint/keyring/connect failed after we reserved the slot — roll the reservation back so a failed+ // invite doesn't leak a seat.+ byId.delete(rendererId)+ throw err}}// Legacy entry point (RemoteAccessDialog / RemoteSection). Now ADDITIVE + cap-checked (no supersede)// — with cap 1 this is bit-for-bit today's single-peer behavior (a 2nd start is refused until the- // first drops), and with cap N it adds a seat.- ipcMain.handle(IPC.relayHostStart, (_e, projectId?: string): Promise<{ offer: string }> =>+ // first drops), and with cap N it adds a seat. Returns `{ offer, id }`; the legacy dialogs+ // destructure `{ offer }` and ignore `id` (additive, non-breaking).+ ipcMain.handle(IPC.relayHostStart, (_e, projectId?: string): Promise<{ offer: string; id: string }> =>addSeat({ projectId }))// Team Access entry point (Settings → Team Access, Task 4): add a seat tagged with the invitee email.+ // Returns `{ offer, id }` — the settings UI uses `id` to show the pending row immediately and revoke it.ipcMain.handle(IPC.relayHostInvite,- (_e, opts: AddSeatOptions = {}): Promise<{ offer: string }> =>+ (_e, opts: AddSeatOptions = {}): Promise<{ offer: string; id: string }> =>addSeat({ projectId: opts?.projectId, email: opts?.email }))// The human compared the SAS and pressed Confirm → advance the trust gate for THAT peer. Only the- // session named by the id is touched; an unknown id (stale event) is a no-op.+ // session named by the id is touched; an unknown id (stale event) or a reserved-not-yet-open seat is+ // a no-op.ipcMain.on(IPC.relayHostConfirm, (_e, msg: { id?: string } = {}) => {- const session = msg?.id ? byId.get(msg.id) : undefined- session?.confirm()+ const entry = msg?.id ? byId.get(msg.id) : undefined+ entry?.session?.confirm()})// Per-peer revoke (Team Access, closes the 4c follow-up): cut ONE bridged peer's LIVE socket- // immediately and free its seat. Only the session named by the id is touched; an unknown/stale id is- // a no-op.+ // immediately and free its seat. Works for BOTH a live peer AND a reserved-but-never-connected seat+ // (the id exists from mint). Only the seat named by the id is touched; an unknown/stale id is a no-op.ipcMain.on(IPC.relayHostRevoke, (_e, msg: { id?: string } = {}) => {- const session = msg?.id ? byId.get(msg.id) : undefined- if (!session) return- const peerKeyB64 = session.peerKeyB64()- // Cut the live socket by peer IDENTITY — the same primitive index.ts's 4c revoker uses- // (killRelayHostsByPeerKey, relay-host.ts). It closes every live session holding that key, which- // is the right "remove this device" semantic. Host-side revoke + the seat cap above are UX/host- // enforcement, NOT a server-guaranteed limit (v2 = server-side). On the desktop relay path a pin- // never auto-admits (isPinned is phone-only), so cutting the socket already forces a fresh- // SAS+consent re-pair — no separate unpin is needed to keep the removed device out.- if (peerKeyB64) killRelayHostsByPeerKey(peerKeyB64)+ const entry = msg?.id ? byId.get(msg.id) : undefined+ if (!entry) return+ // A reserved-but-not-yet-open seat has no live socket to cut (`session` null, or a session with no+ // peer key yet) — freeing the reservation is all that's owed. A live peer is cut by IDENTITY, the+ // same primitive index.ts's 4c revoker uses (killRelayHostsByPeerKey, relay-host.ts): it closes+ // every live session holding that key, the right "remove this device" semantic. Host-side revoke ++ // the seat cap are UX/host enforcement, NOT a server-guaranteed limit (v2 = server-side). On the+ // desktop relay path a pin never auto-admits (isPinned is phone-only), so cutting the socket+ // already forces a fresh SAS+consent re-pair — no separate unpin is needed.+ const peerKeyB64 = entry.session?.peerKeyB64()// killRelayHostsByPeerKey → session.close() does NOT fire our `onClose` (that runs only on a wire// drop, not a local close), so free the seat and notify the renderer here.- forget(session)- pool.delete(session)+ if (peerKeyB64) killRelayHostsByPeerKey(peerKeyB64)+ byId.delete(msg.id!)send(IPC.relayHostClosed, { id: msg.id })})const stop = (): void => {- for (const session of pool.keys()) session.close()- pool.clear()+ for (const { session } of byId.values()) session?.close()byId.clear()}diff --git a/src/shared/types.ts b/src/shared/types.tsindex 3171d313d..727ba58d6 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -1166,14 +1166,15 @@ export interface RelayHostApi {* Rejects if the device is not entitled (or a dev build without the relay URL). `projectId` is the* single project this hosting session shares with the peer; omit for the legacy whole-workspace view.*/- start(projectId?: string): Promise<{ offer: string }>+ start(projectId?: string): Promise<{ offer: string; id: string }>/*** Team Access: ADD a seat — mint a fresh pairing offer for one more device (no supersede), tagged* with the optional invitee `email` (display label only). Rejects `E_SEATS_FULL` when the licensed* seat cap is reached, and with the Pro / dev-build errors `start` uses. `projectId` scopes the- * shared project as in `start`.+ * shared project as in `start`. Resolves with the offer AND the seat's `id` — the settings UI uses+ * it to show the pending row immediately and to `revoke` a seat whose peer never connects.*/- invite(opts?: { projectId?: string; email?: string }): Promise<{ offer: string }>+ invite(opts?: { projectId?: string; email?: string }): Promise<{ offer: string; id: string }>/** Leave host mode: close every bridged peer in the pool. */stop(): Promise<void>/**