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): scope the workspace:load response to the shared project (project-scope Task 2)

enes3d48b30ddee9
diff --git a/src/main/remote/relay-host.test.ts b/src/main/remote/relay-host.test.ts
index 84e65f50c..50c67d7c0 100644
--- a/src/main/remote/relay-host.test.ts
+++ b/src/main/remote/relay-host.test.ts
@@ -82,7 +82,10 @@ let platform: ElectronPlatform
* A host session bridged to a REAL peer relay socket over an in-process transport pair. `buffered`
* is the host transport's ws.bufferedAmount — the number the sink must surface.
*/
-function openHostAgainstFakeRelay(opts?: { bufferedAmount?: () => number }): {
+function openHostAgainstFakeRelay(opts?: {
+ bufferedAmount?: () => number
+ sharedProjectId?: string
+}): {
session: RelayHostSession
peerKeyB64: string
textFrames: string[]
@@ -142,6 +145,7 @@ function openHostAgainstFakeRelay(opts?: { bufferedAmount?: () => number }): {
ourKeys: hostKeys,
platform,
transport: hostT,
+ sharedProjectId: opts?.sharedProjectId,
onPeerPending: (s) => pending.push(s),
onOpen: (s) => opens.push(s),
onClose: () => {
@@ -348,6 +352,62 @@ describe('relay host — presence, canvas and RPC reach a bridged peer', () => {
})
})
+describe('relay host — workspace:load is scoped to the shared project', () => {
+ const threeProjectWorkspace = () => ({
+ version: 2,
+ activeProjectId: 'proj-0',
+ projects: [
+ { id: 'proj-0', name: 'zero', color: '#000', nodes: [], viewport: { x: 0, y: 0, zoom: 1 } },
+ { id: 'proj-1', name: 'one', color: '#111', nodes: [], viewport: { x: 0, y: 0, zoom: 1 } },
+ { id: 'proj-2', name: 'two', color: '#222', nodes: [], viewport: { x: 0, y: 0, zoom: 1 } }
+ ]
+ })
+
+ it('returns ONLY the shared project (activeProjectId retargeted) over a scoped session', async () => {
+ platform.handle(IPC.workspaceLoad, async () => threeProjectWorkspace())
+ const s = openHostAgainstFakeRelay({ sharedProjectId: 'proj-1' })
+ await s.openMutually()
+
+ s.peerSendsTunnelText(JSON.stringify({ t: 'req', id: 7, method: IPC.workspaceLoad, args: [] }))
+ await vi.waitFor(() => {
+ const res = JSON.parse(s.textFrames.at(-1)!)
+ expect(res).toMatchObject({ t: 'res', id: 7, ok: true })
+ expect(res.result.projects.map((p: any) => p.id)).toEqual(['proj-1'])
+ expect(res.result.activeProjectId).toBe('proj-1')
+ })
+ })
+
+ it('leaves a NON-workspace method (git:status) untouched on a scoped session', async () => {
+ platform.handle(IPC.gitStatus, async () => ({ branch: 'main', files: [] }))
+ const s = openHostAgainstFakeRelay({ sharedProjectId: 'proj-1' })
+ await s.openMutually()
+
+ s.peerSendsTunnelText(JSON.stringify({ t: 'req', id: 8, method: IPC.gitStatus, args: ['/w'] }))
+ await vi.waitFor(() =>
+ expect(JSON.parse(s.textFrames.at(-1)!)).toMatchObject({
+ t: 'res',
+ id: 8,
+ ok: true,
+ result: { branch: 'main', files: [] }
+ })
+ )
+ })
+
+ it('with NO sharedProjectId returns the FULL workspace unchanged', async () => {
+ platform.handle(IPC.workspaceLoad, async () => threeProjectWorkspace())
+ const s = openHostAgainstFakeRelay() // unscoped — legacy behaviour
+ await s.openMutually()
+
+ s.peerSendsTunnelText(JSON.stringify({ t: 'req', id: 9, method: IPC.workspaceLoad, args: [] }))
+ await vi.waitFor(() => {
+ const res = JSON.parse(s.textFrames.at(-1)!)
+ expect(res).toMatchObject({ t: 'res', id: 9, ok: true })
+ expect(res.result.projects.map((p: any) => p.id)).toEqual(['proj-0', 'proj-1', 'proj-2'])
+ expect(res.result.activeProjectId).toBe('proj-0')
+ })
+ })
+})
+
describe('relay host — nothing is served before mutual approval', () => {
it('a peer RPC BEFORE mutual approval is refused (no dispatch, no sink, no presence)', async () => {
let created = 0
diff --git a/src/main/remote/relay-host.ts b/src/main/remote/relay-host.ts
index 2f083b4ec..8be718853 100644
--- a/src/main/remote/relay-host.ts
+++ b/src/main/remote/relay-host.ts
@@ -28,7 +28,10 @@ import type { KeyPair } from './e2ee'
import type { ElectronPlatform } from '../platform-electron'
import { registerPeerSink, unregisterPeerSink } from '../peer-registry'
import { allocateRelayClientId, presenceHub } from '../../core/presence/hub'
-import { E_UNAUTHORIZED, parseRpcMessage } from '../../shared/rpc'
+import { E_UNAUTHORIZED, parseRpcMessage, type RpcErr, type RpcOk } from '../../shared/rpc'
+import { IPC } from '../../shared/ipc'
+import { scopeWorkspaceToProject } from '../../shared/relay-workspace-scope'
+import type { Workspace } from '../../shared/types'
export interface RelayHostSession {
/** The peer's presence/platform ClientId once it is open, else null. */
@@ -154,6 +157,16 @@ export function connectRelayHost(opts: ConnectRelayHostOptions): RelayHostSessio
opts.onOpen(session)
}
+ /** UX scope, NOT a trust boundary: for the ONE `workspace:load` method, when this hosting session
+ * is bound to a single project, narrow the successful response to that project (see
+ * scopeWorkspaceToProject). Every other method — and an error response, and an unscoped session —
+ * passes through byte-identical. This can only NARROW: it never exposes anything the core did not
+ * already return, and it never touches a non-`workspace:load` response. */
+ const scopeResponse = (method: string, res: RpcOk | RpcErr): RpcOk | RpcErr => {
+ if (!opts.sharedProjectId || method !== IPC.workspaceLoad || res.ok !== true) return res
+ return { ...res, result: scopeWorkspaceToProject(res.result as Workspace, opts.sharedProjectId) }
+ }
+
const socket = connectRelay({
url: opts.url,
token: opts.token,
@@ -210,7 +223,7 @@ export function connectRelayHost(opts: ConnectRelayHostOptions): RelayHostSessio
const id = clientId
void opts.platform
.dispatch(id, m)
- .then((res) => socket.sendTunnelText(JSON.stringify(res)))
+ .then((res) => socket.sendTunnelText(JSON.stringify(scopeResponse(m.method, res))))
} else if (m.t === 'cast') {
opts.platform.cast(clientId, m.method, m.args)
}
diff --git a/src/shared/relay-workspace-scope.test.ts b/src/shared/relay-workspace-scope.test.ts
new file mode 100644
index 000000000..77c67067a
--- /dev/null
+++ b/src/shared/relay-workspace-scope.test.ts
@@ -0,0 +1,43 @@
+import { describe, it, expect } from 'vitest'
+import { scopeWorkspaceToProject } from './relay-workspace-scope'
+import type { Project, Workspace } from './types'
+
+const project = (id: string): Project => ({
+ id,
+ name: id,
+ color: '#fff',
+ nodes: [],
+ viewport: { x: 0, y: 0, zoom: 1 }
+})
+
+const ws = (): Workspace => ({
+ version: 2,
+ activeProjectId: 'a',
+ projects: [project('a'), project('b'), project('c')]
+})
+
+describe('scopeWorkspaceToProject', () => {
+ it('keeps only the named project and points activeProjectId at it', () => {
+ const scoped = scopeWorkspaceToProject(ws(), 'b')
+ expect(scoped.projects.map((p) => p.id)).toEqual(['b'])
+ expect(scoped.activeProjectId).toBe('b')
+ })
+
+ it('returns an empty-projects workspace when the id is gone', () => {
+ const scoped = scopeWorkspaceToProject(ws(), 'missing')
+ expect(scoped.projects).toEqual([])
+ expect(scoped.activeProjectId).toBe('')
+ })
+
+ it('preserves every other top-level workspace field as-is', () => {
+ const scoped = scopeWorkspaceToProject(ws(), 'b')
+ expect(scoped.version).toBe(2)
+ })
+
+ it('does not mutate the input workspace', () => {
+ const input = ws()
+ scopeWorkspaceToProject(input, 'b')
+ expect(input.projects.map((p) => p.id)).toEqual(['a', 'b', 'c'])
+ expect(input.activeProjectId).toBe('a')
+ })
+})
diff --git a/src/shared/relay-workspace-scope.ts b/src/shared/relay-workspace-scope.ts
new file mode 100644
index 000000000..b423f75c4
--- /dev/null
+++ b/src/shared/relay-workspace-scope.ts
@@ -0,0 +1,15 @@
+import type { Workspace } from './types'
+
+/**
+ * Narrow a Workspace to the single project a relay hosting session shares with its peer.
+ *
+ * Returns a Workspace containing ONLY the project whose `id === projectId`, with
+ * `activeProjectId` pointing at it. If no project matches (it was deleted/closed since
+ * hosting started), the projects list is empty and `activeProjectId` is `''`. Every other
+ * top-level field is carried through untouched. This only ever NARROWS — it can never expose
+ * a project the source workspace did not already contain. Pure (does not mutate the input).
+ */
+export function scopeWorkspaceToProject(ws: Workspace, projectId: string): Workspace {
+ const projects = ws.projects.filter((p) => p.id === projectId)
+ return { ...ws, projects, activeProjectId: projects.length > 0 ? projectId : '' }
+}