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): populate the relay tab from the host's shared project (project-scope Task 4)

enesdafda0f15240
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index e6f5d1c03..8ff3cbb26 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -1860,6 +1860,11 @@ export function Canvas() {
addProject: reconnectProjectId
? () => ({ id: reconnectProjectId }) // reconnect: reuse the existing tab, don't spawn one
: (name) => useProjects.getState().addProject(name),
+ // First connect adopts the host's shared project (its nodes, fresh id). On reconnect the
+ // existing tab (with its nodes) is reused via addProject above, so no adopt lever is passed.
+ adoptProject: reconnectProjectId
+ ? undefined
+ : (p) => useProjects.getState().adoptProject(p),
setActiveProject: (id) => useProjects.getState().setActive(id),
})
.then((tab) => {
diff --git a/src/renderer/session/relay-tab.test.ts b/src/renderer/session/relay-tab.test.ts
index 77a0c2208..11aa621c5 100644
--- a/src/renderer/session/relay-tab.test.ts
+++ b/src/renderer/session/relay-tab.test.ts
@@ -15,7 +15,7 @@ import {
resetSessionsForTest,
} from './session'
import { LocalTransport } from '../terminal/local-transport'
-import type { NodeTerminalApi } from '@shared/types'
+import type { NodeTerminalApi, Project, Workspace } from '@shared/types'
import type { RelayApiHandle } from '../bridge/relay-api'
/** A fake relayClient.onClosed sink: keeps the callback so the test can fire a socket drop. */
@@ -35,36 +35,41 @@ function fakeRelayClient() {
/** A bridged relay api whose `pty.create` is a spy — this is what `LocalTransport(api)` must hit.
* `presenceUnsub` is what the session's held presence teardown calls (onSync's unsubscribe), so a
* drop that "runs the presence teardown" is observable in node env. */
-function fakeBridgedApi() {
+function fakeBridgedApi(ws: Workspace = { version: 2, activeProjectId: '', projects: [] }) {
const ptyCreate = vi.fn().mockResolvedValue({ sessionId: 's1', fresh: false })
const presenceUnsub = vi.fn()
+ const workspaceLoad = vi.fn().mockResolvedValue(ws)
const api = {
marker: 'relay-bridged',
pty: { create: ptyCreate },
+ workspace: { load: workspaceLoad },
presence: {
hello: vi.fn().mockResolvedValue({ clientId: 'x', peers: [] }),
onSync: vi.fn(() => presenceUnsub),
onPeer: vi.fn(() => () => {}),
},
} as unknown as NodeTerminalApi
- return { api, ptyCreate, presenceUnsub }
+ return { api, ptyCreate, presenceUnsub, workspaceLoad }
}
function makeDeps(over: Partial<RelayTabDeps> & { handle: RelayApiHandle }): {
deps: RelayTabDeps
addProject: ReturnType<typeof vi.fn>
+ adoptProject: ReturnType<typeof vi.fn>
setActiveProject: ReturnType<typeof vi.fn>
} {
const addProject = vi.fn((_label: string) => ({ id: 'proj-1' }))
+ const adoptProject = vi.fn((p: Project) => ({ id: `${p.id}-adopted` }))
const setActiveProject = vi.fn()
const deps: RelayTabDeps = {
relayClient: over.relayClient ?? fakeRelayClient(),
addProject: over.addProject ?? addProject,
+ adoptProject: 'adoptProject' in over ? over.adoptProject : adoptProject,
setActiveProject: over.setActiveProject ?? setActiveProject,
buildApi: () => over.handle,
timeoutMs: over.timeoutMs,
}
- return { deps, addProject, setActiveProject }
+ return { deps, addProject, adoptProject, setActiveProject }
}
beforeEach(() => {
@@ -104,6 +109,50 @@ describe('openRelayTab (connect → tab → mount)', () => {
expect(sessionCount()).toBe(1) // only local remains
})
+ it('populates the tab by ADOPTING the host\'s scoped project (nodes intact, remote:true)', async () => {
+ const hostProject = {
+ id: 'host-proj',
+ name: "Ayşe's Project",
+ color: '#fff',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: [
+ { id: 'terminal-a', kind: 'terminal', title: 'A', color: '#111', position: { x: 0, y: 0 } },
+ { id: 'terminal-b', kind: 'terminal', title: 'B', color: '#222', position: { x: 1, y: 1 } },
+ ],
+ } as unknown as Project
+ const { api } = fakeBridgedApi({ version: 2, activeProjectId: 'host-proj', projects: [hostProject] })
+ const handle: RelayApiHandle = { api, ready: () => Promise.resolve(), close: vi.fn() }
+ const { deps, addProject, adoptProject, setActiveProject } = makeDeps({ handle })
+
+ const tab = await openRelayTab('conn-1', "Ayşe's Mac", deps)
+
+ // Adopted (not an empty addProject) with the host's nodes and remote:true.
+ expect(addProject).not.toHaveBeenCalled()
+ expect(adoptProject).toHaveBeenCalledTimes(1)
+ const adopted = adoptProject.mock.calls[0][0] as Project
+ expect(adopted.remote).toBe(true)
+ expect(adopted.name).toBe("Ayşe's Project")
+ expect(adopted.nodes.map((n) => n.id)).toEqual(['terminal-a', 'terminal-b'])
+
+ // The relay session is bound to the ADOPTED (fresh) project id, and that tab is active.
+ expect(tab.projectId).toBe('host-proj-adopted')
+ expect(sessionForProject('host-proj-adopted').id).toBe(tab.sessionId)
+ expect(setActiveProject).toHaveBeenCalledWith('host-proj-adopted')
+ })
+
+ it('falls back to an empty labelled tab when the host shared nothing (no throw)', async () => {
+ const { api } = fakeBridgedApi({ version: 2, activeProjectId: '', projects: [] })
+ const handle: RelayApiHandle = { api, ready: () => Promise.resolve(), close: vi.fn() }
+ const { deps, addProject, adoptProject } = makeDeps({ handle })
+
+ const tab = await openRelayTab('conn-1', 'Empty host', deps)
+
+ expect(adoptProject).not.toHaveBeenCalled()
+ expect(addProject).toHaveBeenCalledWith('Empty host')
+ expect(tab.projectId).toBe('proj-1')
+ expect(sessionForProject('proj-1').source).toBe('relay')
+ })
+
it('GUARD: a pre-approval socket drop REJECTS the bootstrap (never hangs) and closes the handle', async () => {
const { api } = fakeBridgedApi()
const close = vi.fn()
diff --git a/src/renderer/session/relay-tab.ts b/src/renderer/session/relay-tab.ts
index 61ce5f7b1..2a8a4f6c3 100644
--- a/src/renderer/session/relay-tab.ts
+++ b/src/renderer/session/relay-tab.ts
@@ -14,7 +14,7 @@
// forever — a dead tab that never errors. So we RACE `ready()` against the connection's real
// close signal (relayClient.onClosed) plus a timeout backstop, and reject on either.
-import type { RelayClientApi } from '@shared/types'
+import type { Project, RelayClientApi } from '@shared/types'
import { buildRelayApi, type RelayApiHandle } from '../bridge/relay-api'
import {
createSession,
@@ -33,8 +33,14 @@ const APPROVAL_TIMEOUT_MS = 60_000
export interface RelayTabDeps {
/** The preload relay surface — only `onClosed` is needed here (to catch a pre-approval drop). */
relayClient: Pick<RelayClientApi, 'onClosed'>
- /** Add a project tab (`useProjects.getState().addProject`) → the new project's id. */
+ /** Add an EMPTY project tab (`useProjects.getState().addProject`) → the new project's id. The
+ * fallback when the host shared nothing (no scoped project) and the reconnect lever (Canvas
+ * overrides it to reuse the existing project id in place). */
addProject: (label: string) => { id: string }
+ /** Adopt the host's shared project as a tab (`useProjects.getState().adoptProject`) → fresh
+ * project id, node ids kept. Used on FIRST connect to populate the tab with the host's actual
+ * nodes. Omitted on reconnect, where the existing tab (and its nodes) is reused via `addProject`. */
+ adoptProject?: (project: Project) => { id: string }
/** Make the new tab active (`useProjects.getState().setActive`). */
setActiveProject: (projectId: string) => void
/** TEST SEAM: build the relay api handle. Production omits it → `buildRelayApi`. */
@@ -76,14 +82,25 @@ export async function openRelayTab(
holdSessionTeardown(session.id, getSessionStores(session.id).presence.connect())
holdSessionTeardown(session.id, () => handle.close())
- const project = deps.addProject(label)
- bindProjectToSession(project.id, session.id)
+ // Populate the tab from the host's workspace, which the relay boundary already SCOPED to the one
+ // shared project (Task 2). Adopt that single project — fresh project id, node ids kept — so the
+ // Canvas active-project effect loads the host's nodes and the SessionProvider routes their
+ // transport to the relay api; Stage-3 sync then keeps it live. If the host shared nothing (or the
+ // project was deleted), OR we're reconnecting (no `adoptProject` dep — the existing tab is reused),
+ // fall back to the labelled tab so it still opens rather than throwing.
+ const ws = await handle.api.workspace.load()
+ const hostProject = ws.projects[0]
+ const projectId =
+ hostProject && deps.adoptProject
+ ? deps.adoptProject({ ...hostProject, remote: true }).id
+ : deps.addProject(label).id
+ bindProjectToSession(projectId, session.id)
setActiveSession(session.id)
- deps.setActiveProject(project.id)
+ deps.setActiveProject(projectId)
return {
sessionId: session.id,
- projectId: project.id,
+ projectId,
dispose: () => disposeSession(session.id),
}
}