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(relay): IPC channels + preload surfaces for the new tunnel (relayHost/relayClient)

enesfa99ab7fff78
diff --git a/src/preload/index.ts b/src/preload/index.ts
index eba73a111..07bce6086 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -7,6 +7,7 @@ import type {
Project,
PtyCreateOptions,
RecycledInfo,
+ RelayPeerPending,
UpdateInfo,
UpdateProgress,
Workspace,
@@ -43,6 +44,12 @@ const subscribeMutation = subscribe<[CanvasMutation]>(IPC.remoteHostApplyMutatio
// finishes the handshake; carries the SAS to show in the approval dialog).
const subscribePeerPending = subscribe<[{ sas: string | null; id: string }]>(IPC.remoteHostPeerPending)
+// New relay tunnel (Stage 4). Non-per-id host events reuse the fan-out helper; per-connection
+// client events (sas/approved/frame/closed) attach directly, like remoteClient's per-session ones.
+const subscribeRelayPeerPending = subscribe<[RelayPeerPending]>(IPC.relayHostPeerPending)
+const subscribeRelayHostOpen = subscribe<[{ id: string }]>(IPC.relayHostOpen)
+const subscribeRelayHostClosed = subscribe<[{ id: string }]>(IPC.relayHostClosed)
+
const api: NodeTerminalApi = {
pty: {
create: (options: PtyCreateOptions) => ipcRenderer.invoke(IPC.ptyCreate, options),
@@ -407,6 +414,44 @@ const api: NodeTerminalApi = {
fsWrite: (connectionId, path, content) =>
ipcRenderer.invoke(IPC.remoteClientFsWrite, connectionId, path, content)
},
+ relayHost: {
+ start: () => ipcRenderer.invoke(IPC.relayHostStart),
+ stop: () => ipcRenderer.invoke(IPC.relayHostStop),
+ onPeerPending: subscribeRelayPeerPending,
+ confirm: (id: string) => ipcRenderer.send(IPC.relayHostConfirm, { id }),
+ onOpen: subscribeRelayHostOpen,
+ onClosed: subscribeRelayHostClosed
+ },
+ relayClient: {
+ connect: (offer) => ipcRenderer.invoke(IPC.relayClientConnect, offer),
+ onSas: (connectionId, listener) => {
+ const channel = IPC.relayClientSas(connectionId)
+ const handler = (_e: unknown, sas: string | null) => listener(sas)
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ confirm: (connectionId) => ipcRenderer.send(IPC.relayClientConfirm, { id: connectionId }),
+ onApproved: (connectionId, listener) => {
+ const channel = IPC.relayClientApproved(connectionId)
+ const handler = () => listener()
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ send: (connectionId, frame) => ipcRenderer.send(IPC.relayClientSend, connectionId, frame),
+ onFrame: (connectionId, listener) => {
+ const channel = IPC.relayClientFrame(connectionId)
+ const handler = (_e: unknown, frame: string) => listener(frame)
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ onClosed: (connectionId, listener) => {
+ const channel = IPC.relayClientClosed(connectionId)
+ const handler = () => listener()
+ ipcRenderer.on(channel, handler)
+ return () => ipcRenderer.removeListener(channel, handler)
+ },
+ disconnect: (connectionId) => ipcRenderer.send(IPC.relayClientDisconnect, connectionId)
+ },
handoff: {
build: (sessionId, agentId, sourceNodeId, cwd, accountId) =>
ipcRenderer.invoke(IPC.handoffBuild, sessionId, agentId, sourceNodeId, cwd, accountId)
diff --git a/src/renderer/bridge/stubs.test.ts b/src/renderer/bridge/stubs.test.ts
index 419d97d42..1b98212f7 100644
--- a/src/renderer/bridge/stubs.test.ts
+++ b/src/renderer/bridge/stubs.test.ts
@@ -29,6 +29,34 @@ describe('bridge stubs', () => {
}
})
+ it('relay tunnel is a desktop-only unsupported degrade (subscriptions still safe)', async () => {
+ const s = buildStubApi()
+ // Entry points reject with the coded error so the renderer hides the affordance.
+ await expect(s.relayHost.start()).rejects.toMatchObject({ code: E_UNSUPPORTED })
+ await expect(s.relayHost.stop()).rejects.toMatchObject({ code: E_UNSUPPORTED })
+ await expect(s.relayClient.connect('offer')).rejects.toMatchObject({ code: E_UNSUPPORTED })
+ // Subscriptions must return callable no-op unsubscribes (used as React effect cleanups).
+ for (const un of [
+ s.relayHost.onPeerPending(() => {}),
+ s.relayHost.onOpen(() => {}),
+ s.relayHost.onClosed(() => {}),
+ s.relayClient.onSas('c', () => {}),
+ s.relayClient.onApproved('c', () => {}),
+ s.relayClient.onFrame('c', () => {}),
+ s.relayClient.onClosed('c', () => {})
+ ]) {
+ expect(typeof un).toBe('function')
+ expect(() => un()).not.toThrow()
+ }
+ // Void gate/frame members are inert (no connection ever exists here).
+ expect(() => {
+ s.relayHost.confirm('id')
+ s.relayClient.confirm('c')
+ s.relayClient.send('c', '{}')
+ s.relayClient.disconnect('c')
+ }).not.toThrow()
+ })
+
it('boot-path promise members resolve benignly', async () => {
const s = buildStubApi()
await expect(s.announcements.fetch()).resolves.toEqual([])
diff --git a/src/renderer/bridge/stubs.ts b/src/renderer/bridge/stubs.ts
index 2e06e8821..895ac6eb5 100644
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@ -271,6 +271,29 @@ export function buildStubApi(): Omit<
fsReadBinary: U('remoteClient.fsReadBinary'),
fsWrite: U('remoteClient.fsWrite')
},
+ // New relay tunnel (Stage 4). Hosting/connecting a peer-to-peer relay is a desktop-only
+ // (Electron) capability — the Server Edition is itself the remote host, reached over its own WS
+ // bridge — so the entry points (`start`/`connect`) reject with E_UNSUPPORTED and the UI hides
+ // the affordance. Because those never yield a live connection, the gate/frame void members are
+ // inert no-ops (there is no connectionId to act on) and the subscriptions are no-op unsubscribes.
+ relayHost: {
+ start: U('relayHost.start'),
+ stop: U('relayHost.stop'),
+ onPeerPending: noopUnsub,
+ confirm: noop,
+ onOpen: noopUnsub,
+ onClosed: noopUnsub
+ },
+ relayClient: {
+ connect: U('relayClient.connect'),
+ onSas: noopUnsub,
+ confirm: noop,
+ onApproved: noopUnsub,
+ send: noop,
+ onFrame: noopUnsub,
+ onClosed: noopUnsub,
+ disconnect: noop
+ },
handoff: {
build: U('handoff.build')
},
diff --git a/src/shared/ipc.test.ts b/src/shared/ipc.test.ts
new file mode 100644
index 000000000..d613ca6fd
--- /dev/null
+++ b/src/shared/ipc.test.ts
@@ -0,0 +1,51 @@
+import { describe, it, expect } from 'vitest'
+import { IPC } from './ipc'
+
+/** Resolve every IPC entry to its concrete channel string(s): plain strings pass through, and the
+ * per-id factory functions (e.g. `ptyData(sessionId)`) are invoked with a sample id. */
+function allChannels(): string[] {
+ const out: string[] = []
+ for (const value of Object.values(IPC)) {
+ if (typeof value === 'string') out.push(value)
+ else if (typeof value === 'function') out.push((value as (...a: string[]) => string)('sample', 'sample'))
+ }
+ return out
+}
+
+describe('IPC channels', () => {
+ it('every channel string is unique', () => {
+ const channels = allChannels()
+ expect(new Set(channels).size).toBe(channels.length)
+ })
+
+ it('exposes the new relay tunnel host channels (distinct from the legacy remote* dialect)', () => {
+ expect(IPC.relayHostStart).toBe('relay:host:start')
+ expect(IPC.relayHostStop).toBe('relay:host:stop')
+ expect(IPC.relayHostPeerPending).toBe('relay:host:peer-pending')
+ expect(IPC.relayHostConfirm).toBe('relay:host:confirm')
+ expect(IPC.relayHostOpen).toBe('relay:host:open')
+ expect(IPC.relayHostClosed).toBe('relay:host:closed')
+ // The new tunnel MUST NOT reuse the legacy `remote:*` namespace (both coexist until Task 10).
+ for (const ch of [
+ IPC.relayHostStart,
+ IPC.relayHostStop,
+ IPC.relayHostPeerPending,
+ IPC.relayHostConfirm,
+ IPC.relayHostOpen,
+ IPC.relayHostClosed
+ ]) {
+ expect(ch.startsWith('relay:')).toBe(true)
+ }
+ })
+
+ it('exposes the new relay tunnel client channels, with per-id factories', () => {
+ expect(IPC.relayClientConnect).toBe('relay:client:connect')
+ expect(IPC.relayClientConfirm).toBe('relay:client:confirm')
+ expect(IPC.relayClientSend).toBe('relay:client:send')
+ expect(IPC.relayClientDisconnect).toBe('relay:client:disconnect')
+ expect(IPC.relayClientSas('abc')).toBe('relay:client:sas:abc')
+ expect(IPC.relayClientApproved('abc')).toBe('relay:client:approved:abc')
+ expect(IPC.relayClientFrame('abc')).toBe('relay:client:frame:abc')
+ expect(IPC.relayClientClosed('abc')).toBe('relay:client:closed:abc')
+ })
+})
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index 28e0ea4a5..62c4ba8be 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -240,6 +240,38 @@ export const IPC = {
`remote:client:exit:${connectionId}:${streamId}`,
// Fired when a connection's relay socket drops (host/relay gone).
remoteClientClosed: (connectionId: string) => `remote:client:closed:${connectionId}`,
+ // ── New E2EE relay tunnel (Stage 4) ─────────────────────────────────────────────────────────
+ // The successor to the legacy `remote:host:*` / `remote:client:*` dialect above. Both coexist
+ // during the migration (the phone still speaks the old channels until Task 10 deletes them), so
+ // these deliberately use a distinct `relay:*` namespace. A connected peer is a first-class
+ // CorePlatform client: the client casts raw rpc.ts frames (JSON strings) at the host and receives
+ // frames back, rather than a bespoke per-verb channel set.
+ //
+ // HOST side: enter/leave host mode, and the mutual-approval gate. `relayHostPeerPending` fires
+ // main → renderer when a client finishes the encrypted handshake and is awaiting approval
+ // (payload `{ id, sas, peerKeyB64 }` — the SAS both humans compare, the peer's box key to pin);
+ // the host human answers with `relayHostConfirm` (id). `relayHostOpen` / `relayHostClosed` fire
+ // main → renderer when a bridged peer becomes a live client / drops (payload `{ id }`).
+ relayHostStart: 'relay:host:start',
+ relayHostStop: 'relay:host:stop',
+ relayHostPeerPending: 'relay:host:peer-pending',
+ relayHostConfirm: 'relay:host:confirm',
+ relayHostOpen: 'relay:host:open',
+ relayHostClosed: 'relay:host:closed',
+ // CLIENT side: connect to a host by its pairing offer (resolves a connectionId), the client half
+ // of the same mutual-approval gate, and the raw frame pipe. `relayClientSas` pushes the channel
+ // SAS main → renderer so the client human can compare it before the host approves;
+ // `relayClientConfirm` (id) is this human's confirmation; `relayClientApproved` fires once the
+ // host approves. `relayClientSend` casts an outbound rpc frame (JSON) at the host;
+ // `relayClientFrame` delivers an inbound one. `relayClientClosed` fires when the socket drops.
+ relayClientConnect: 'relay:client:connect',
+ relayClientConfirm: 'relay:client:confirm',
+ relayClientSend: 'relay:client:send',
+ relayClientDisconnect: 'relay:client:disconnect',
+ relayClientSas: (connectionId: string) => `relay:client:sas:${connectionId}`,
+ relayClientApproved: (connectionId: string) => `relay:client:approved:${connectionId}`,
+ relayClientFrame: (connectionId: string) => `relay:client:frame:${connectionId}`,
+ relayClientClosed: (connectionId: string) => `relay:client:closed:${connectionId}`,
handoffBuild: 'handoff:build',
// Phone pairing (nodeterm iOS "scan a QR" flow): renderer starts/stops the one-shot LAN
// listener; main pushes the completion result back over `pairing:done`. The per-device
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 8aa9337fb..fd2787262 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1163,6 +1163,76 @@ export interface RemoteClientApi {
fsWrite(connectionId: string, path: string, content: string): Promise<boolean>
}
+/**
+ * Payload of `relayHost.onPeerPending`: a client has finished the E2EE handshake over the new
+ * relay tunnel and is awaiting the host human's approval. `id` addresses this pending peer for
+ * `confirm(id)`; `sas` is the channel verification code both humans compare (null before the key is
+ * derived); `peerKeyB64` is the peer's stable box public key to pin on approval.
+ */
+export interface RelayPeerPending {
+ id: string
+ sas: string | null
+ peerKeyB64: string
+}
+
+/**
+ * HOST side of the new E2EE relay tunnel (Stage 4) — the successor to `RemoteHostApi`. A connected
+ * peer becomes a first-class CorePlatform client (it exchanges raw rpc frames), so this surface is
+ * only the mutual-approval gate plus enter/leave, not a per-verb API. Desktop-only (Electron);
+ * the Server Edition browser build degrades every member to `E_UNSUPPORTED`/no-op.
+ */
+export interface RelayHostApi {
+ /**
+ * Enter host mode over the relay: connect and return a pairing offer string to hand to a client.
+ * Rejects if the device is not entitled (or a dev build without the relay URL).
+ */
+ start(): Promise<{ offer: string }>
+ /** Leave host mode: close the relay connection (drops every bridged peer). */
+ stop(): Promise<void>
+ /**
+ * Fires when a client finishes the handshake and is awaiting approval. The host must `confirm()`
+ * before the peer is admitted as a client. Returns an unsubscribe function.
+ */
+ onPeerPending(listener: (info: RelayPeerPending) => void): () => void
+ /** Approve the pending peer (by its pending id) after comparing the SAS → it joins as a client. */
+ confirm(id: string): void
+ /** Fires when a bridged peer becomes a live client (both humans confirmed). Returns unsubscribe. */
+ onOpen(listener: (info: { id: string }) => void): () => void
+ /** Fires when a bridged peer's connection drops. Returns an unsubscribe function. */
+ onClosed(listener: (info: { id: string }) => void): () => void
+}
+
+/**
+ * CLIENT side of the new E2EE relay tunnel (Stage 4) — the successor to `RemoteClientApi`. The
+ * client exchanges raw rpc.ts frames (JSON strings) with the host over the encrypted tunnel rather
+ * than a per-verb channel set. Desktop-only (Electron); the Server Edition browser build degrades
+ * every member to `E_UNSUPPORTED`/no-op.
+ */
+export interface RelayClientApi {
+ /**
+ * Connect to a host by its pairing offer string. Gates on entitlement (rejects otherwise, and in
+ * dev builds without the relay URL). Resolves with a `connectionId` to address the methods below.
+ */
+ connect(offer: string): Promise<string>
+ /**
+ * Listen for the channel SAS once the handshake completes, so the client human can compare it
+ * with the code shown on the host before approving. Returns an unsubscribe function.
+ */
+ onSas(connectionId: string, listener: (sas: string | null) => void): () => void
+ /** Confirm the SAS on this side (the client half of the mutual-approval gate). */
+ confirm(connectionId: string): void
+ /** Fires once the host approves this connection → the client may begin exchanging frames. */
+ onApproved(connectionId: string, listener: () => void): () => void
+ /** Cast an outbound rpc frame (a JSON string) at the host over the tunnel. */
+ send(connectionId: string, frame: string): void
+ /** Listen for an inbound rpc frame (a JSON string) from the host. Returns an unsubscribe. */
+ onFrame(connectionId: string, listener: (frame: string) => void): () => void
+ /** Fires when the connection's relay socket drops (host/relay gone). Returns unsubscribe. */
+ onClosed(connectionId: string, listener: () => void): () => void
+ /** Close a connection: end the relay socket and drop access to the host. */
+ disconnect(connectionId: string): void
+}
+
/** A paired device as exposed to the renderer — the bearer token is never included. */
export interface PairedDevice {
id: string
@@ -1239,6 +1309,8 @@ export interface NodeTerminalApi {
transcripts: TranscriptsApi
remoteHost: RemoteHostApi
remoteClient: RemoteClientApi
+ relayHost: RelayHostApi
+ relayClient: RelayClientApi
handoff: HandoffApi
pairing: PairingApi
presence: PresenceApi