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): tell connected team members when the shared project set changes

review37f1274753c2
diff --git a/src/core/relay/access-policy.test.ts b/src/core/relay/access-policy.test.ts
index 36f869b0c..a1543058a 100644
--- a/src/core/relay/access-policy.test.ts
+++ b/src/core/relay/access-policy.test.ts
@@ -323,6 +323,11 @@ describe('outbound filter: deny by default for non-editors', () => {
expect(filterOutboundEvent(ev(IPC.projectTrustChanged, { projectId: 'Q' }), ctx('viewer'))).toBe(false)
})
+ it('a viewer receives relay:hosted:shared-changed (its tabs follow it; the payload is the shared set)', () => {
+ const shared = JSON.stringify({ t: 'ev', channel: IPC.relayHostedSharedChanged, args: [{ projectIds: ['p1'] }] })
+ expect(filterOutboundEvent(shared, ctx('viewer'))).toBe(true)
+ })
+
it('per-project channels are filtered by the project in their name', () => {
expect(filterOutboundEvent(ev(IPC.boardLogChanged('P'), 'P'), ctx('viewer'))).toBe(true)
expect(filterOutboundEvent(ev(IPC.boardLogChanged('Q'), 'Q'), ctx('viewer'))).toBe(false)
diff --git a/src/core/relay/access-policy.ts b/src/core/relay/access-policy.ts
index 49f5156fa..f8a0ec719 100644
--- a/src/core/relay/access-policy.ts
+++ b/src/core/relay/access-policy.ts
@@ -407,7 +407,9 @@ export const EDITOR_ONLY: ReadonlySet<string> = new Set<string>([
// The hosted team verbs (renderer `buildHostedApi`). INTERCEPTED by hosted-service.ts before this
// table is ever consulted, which judges each caller itself (`self`: any member; the rest: owners
// only) — so this table never decides them. Listed so the guard sees a decision, not a gap. Their
- // events reach owners only (hosted-service `tellOwners`), never through VIEW_EVENTS.
+ // events reach owners only (hosted-service `tellOwners`), never through VIEW_EVENTS — except
+ // `relayHostedSharedChanged`, an event every MEMBER receives (`tellMembers`), so it also has a
+ // VIEW_EVENTS entry.
IPC.relayHostedSelf,
IPC.relayHostedPending,
IPC.relayHostedInviteCode,
@@ -415,6 +417,7 @@ export const EDITOR_ONLY: ReadonlySet<string> = new Set<string>([
IPC.relayHostedDeny,
IPC.relayHostedPeerPending,
IPC.relayHostedPendingClosed,
+ IPC.relayHostedSharedChanged,
// Server→client events (see the doc comment): what a non-editor receives is VIEW_EVENTS.
IPC.workspaceMigrated,
IPC.workspaceCorruptRecovered,
@@ -494,7 +497,9 @@ export const VIEW_EVENTS: Readonly<Record<string, EventCheck>> = Object.freeze({
// Token counts + model for an opaque agent session id: no project, node, path or text.
[IPC.contextUpdate]: always,
[IPC.presenceSync]: always,
- [IPC.presencePeer]: always
+ [IPC.presencePeer]: always,
+ // The shared set itself — exactly what a viewer's narrowed workspace already reveals.
+ [IPC.relayHostedSharedChanged]: always
})
/**
diff --git a/src/core/relay/hosted-service.test.ts b/src/core/relay/hosted-service.test.ts
index e205200ca..79f1db51e 100644
--- a/src/core/relay/hosted-service.test.ts
+++ b/src/core/relay/hosted-service.test.ts
@@ -986,10 +986,11 @@ describe('hosted service — lifecycle', () => {
it('every hosted channel lives under the one prefix the access hook refuses outside the interceptor', () => {
const hosted = Object.entries(IPC).filter(([k]) => k.startsWith('relayHosted')).map(([, v]) => v)
- // Seven relay-tunnel verbs plus the desktop's two bookmark channels. Those two are raw ipcMain
- // handlers that never ride the relay; living under the prefix means a relay peer that asks for
- // one is refused by the access hook here, which is the right answer for them.
- expect(hosted).toHaveLength(9)
+ // Eight relay-tunnel channels (the verbs and their events) plus the desktop's two bookmark
+ // channels. Those two are raw ipcMain handlers that never ride the relay; living under the prefix
+ // means a relay peer that asks for one is refused by the access hook here, which is the right
+ // answer for them.
+ expect(hosted).toHaveLength(10)
expect(hosted).toEqual(expect.arrayContaining([IPC.relayHostedBookmarks, IPC.relayHostedBookmarkRemove]))
for (const ch of hosted) expect(ch).toMatch(/^relay:hosted:/)
})
@@ -1012,6 +1013,30 @@ describe('hosted service — the canvas authority seam (docs/hosted-team-relay.m
expect(seen).toEqual([['P'], ['P', 'Q'], ['Q']])
})
+ it('share and unshare tell every connected member — viewers too — the whole shared set', async () => {
+ const w = world()
+ // ownerOnline shares 'P' first; this set starts empty so each payload is the whole set.
+ const ownerKeys = genKeyPair()
+ await w.svc.init()
+ await w.svc.addOwner(pub(ownerKeys), 'Enes')
+ expect(await w.svc.start()).toBe('started')
+ await vi.waitFor(() => expect(w.svc.status().scheduler?.idle).toBe(1))
+ const owner = w.join(ownerKeys, true)
+ await vi.waitFor(() => expect(owner.isApproved()).toBe(true))
+ const editor = await approvedGuest(w, owner, 'editor', 5)
+ const viewer = await approvedGuest(w, owner, 'viewer', 6)
+ // A device still waiting for an owner is not a member: it is told nothing.
+ const { g: waiting } = await pendingGuest(w)
+ await w.svc.share('p1', true)
+ await w.svc.share('p2', true)
+ await w.svc.share('p1', false)
+ const sets = [{ projectIds: ['p1'] }, { projectIds: ['p1', 'p2'] }, { projectIds: ['p2'] }]
+ await vi.waitFor(() => expect(owner.events(IPC.relayHostedSharedChanged)).toEqual(sets))
+ await vi.waitFor(() => expect(editor.events(IPC.relayHostedSharedChanged)).toEqual(sets))
+ await vi.waitFor(() => expect(viewer.events(IPC.relayHostedSharedChanged)).toEqual(sets))
+ expect(waiting.events(IPC.relayHostedSharedChanged)).toEqual([])
+ })
+
it('a share whose write failed tells nobody', async () => {
let told = 0
const w = world({ onSharedChange: () => told++ })
diff --git a/src/core/relay/hosted-service.ts b/src/core/relay/hosted-service.ts
index 90bd6037c..fdbca590f 100644
--- a/src/core/relay/hosted-service.ts
+++ b/src/core/relay/hosted-service.ts
@@ -24,7 +24,8 @@
// the pin is written (a racing `team add-owner`) keeps it: the approval writes nothing.
// - A relay peer never saves the host's workspace (`workspace:save` is refused for every role): the
// host's canvas authority writes shared projects' content from canvas ops alone, and the share
-// set it governs is read through `sharedProjectIds()` and announced by `onSharedChange`.
+// set it governs is read through `sharedProjectIds()` and announced by `onSharedChange`. Every
+// connected MEMBER (viewers too) is told the whole new set on `relay:hosted:shared-changed`.
// - The scheduler hears about EVERY session end. The core fires `onClose` only for ends the shell
// did not ask for; every end this service causes (deny, expiry, removal, a listener the
// scheduler closes) runs the same `ended` bookkeeping, at most once per session.
@@ -280,6 +281,11 @@ export function createHostedService(deps: HostedServiceDeps): HostedService {
const tellOwners = (channel: string, payload: unknown): void => {
for (const c of conns) if (c.open && memberRole(keyOf(c)) === 'owner') send(c, channel, payload)
}
+ /** Every connected member, judged per send (a removed member gets nothing). Unlike `tellOwners`
+ * this reaches viewers: the payload is only what their narrowed workspace already shows. */
+ const tellMembers = (channel: string, payload: unknown): void => {
+ for (const c of conns) if (c.open && memberRole(keyOf(c)) !== undefined) send(c, channel, payload)
+ }
const pendingList = (): HostedPending[] => [...pending.values()].map((p) => ({ ...p.info }))
const closePending = (id: string, reason: PendingClosedReason): void => {
@@ -626,6 +632,7 @@ export function createHostedService(deps: HostedServiceDeps): HostedService {
},
async share(projectId, on) {
await team.update((d) => setShared(d, projectId, on))
+ tellMembers(IPC.relayHostedSharedChanged, { projectIds: [...team.current().sharedProjects] })
// The share landed; a failing listener must not report it as failed to the admin.
try {
deps.onSharedChange?.()
diff --git a/src/core/relay/scoped-guest-policy.test.ts b/src/core/relay/scoped-guest-policy.test.ts
index 1577e1312..e0e69f5ef 100644
--- a/src/core/relay/scoped-guest-policy.test.ts
+++ b/src/core/relay/scoped-guest-policy.test.ts
@@ -180,6 +180,8 @@ describe('scoped guest — what it receives', () => {
expect(filterScopedEvent(ev(IPC.githubIssuesChanged('alpha'), [1]), s, owners)).toBe(true)
expect(filterScopedEvent(ev(IPC.githubIssuesChanged('beta'), [1]), s, owners)).toBe(false)
expect(filterScopedEvent(ev(IPC.workspaceExternalChange, { id: 'beta' }), s, owners)).toBe(false)
+ // A hosted team's shared set: a viewer of a hosted team receives it, a Team Access guest never.
+ expect(filterScopedEvent(ev(IPC.relayHostedSharedChanged, { projectIds: ['alpha', 'beta'] }), s, owners)).toBe(false)
})
it('delivers terminal bytes only for the shared project\'s sessions', () => {
const hooks = scopedGuestHooks('alpha', deps())
diff --git a/src/core/relay/scoped-guest-policy.ts b/src/core/relay/scoped-guest-policy.ts
index c04f8753b..4a0d3e1b2 100644
--- a/src/core/relay/scoped-guest-policy.ts
+++ b/src/core/relay/scoped-guest-policy.ts
@@ -486,7 +486,8 @@ export const SCOPED_REFUSED: ReadonlySet<string> = new Set<string>([
IPC.relayHostedApprove,
IPC.relayHostedDeny,
IPC.relayHostedPeerPending,
- IPC.relayHostedPendingClosed
+ IPC.relayHostedPendingClosed,
+ IPC.relayHostedSharedChanged
])
function makeScope(projectId: string, deps: ScopedGuestDeps): Scope {
@@ -539,6 +540,9 @@ function scopedExtraEvent(json: string, s: Scope): boolean | null {
const channel = m !== null && typeof m === 'object' ? (m as { channel?: unknown }).channel : undefined
if (typeof channel !== 'string') return null
if (channel === IPC.gitCloneProgress) return true
+ // A hosted team's shared-project list. Its VIEW_EVENTS entry admits it for a hosted team's viewers;
+ // a Team Access guest is scoped to ONE project and must never learn another project's id.
+ if (channel === IPC.relayHostedSharedChanged) return false
const gh = IPC.githubIssuesChanged('')
if (channel.startsWith(gh)) return channel.slice(gh.length) === s.projectId
return null
diff --git a/src/renderer/bridge/relay-api.test.ts b/src/renderer/bridge/relay-api.test.ts
index 092514bb0..68e9c0dac 100644
--- a/src/renderer/bridge/relay-api.test.ts
+++ b/src/renderer/bridge/relay-api.test.ts
@@ -239,6 +239,17 @@ describe('buildRelayApi — hosted team tabs', () => {
expect(seen).toEqual([pending, { pendingId: 'x', reason: 'denied' }])
})
+ it('a shared-set change reaches the onSharedChanged subscribers', () => {
+ const t = new FakeTransport()
+ const { api } = buildRelayApi('conn-1', t, { hosted: true })
+ const seen: unknown[] = []
+ const off = api.hosted!.onSharedChanged((p) => seen.push(p))
+ t.emit(JSON.stringify({ t: 'ev', channel: IPC.relayHostedSharedChanged, args: [{ projectIds: ['p1', 'p2'] }] }))
+ off()
+ t.emit(JSON.stringify({ t: 'ev', channel: IPC.relayHostedSharedChanged, args: [{ projectIds: [] }] }))
+ expect(seen).toEqual([{ projectIds: ['p1', 'p2'] }])
+ })
+
it('before the role is known, a hosted tab sends only what a viewer may (fail closed)', async () => {
const t = new FakeTransport()
const { api } = buildRelayApi('conn-1', t, { hosted: true })
diff --git a/src/renderer/bridge/ws-bridge.ts b/src/renderer/bridge/ws-bridge.ts
index 62dd5f388..db60895e9 100644
--- a/src/renderer/bridge/ws-bridge.ts
+++ b/src/renderer/bridge/ws-bridge.ts
@@ -883,7 +883,8 @@ export function buildPresenceApi(client: RpcClient): Pick<NodeTerminalApi, 'pres
* Edition browser never joins a relay host, and a Team Access relay tab (desktop to desktop) talks
* to a host that answers none of these, so both leave `hosted` absent. The host core answers every
* request itself (src/core/relay/hosted-service.ts) and judges the caller's role: `self` is open to
- * any member, the rest are owner-only. `peer-pending` / `pending-closed` reach connected OWNERS only.
+ * any member, the rest are owner-only. `peer-pending` / `pending-closed` reach connected OWNERS only;
+ * `shared-changed` (the team's whole shared set) reaches every connected member, viewers included.
*/
export function buildHostedApi(client: RpcClient): Required<Pick<NodeTerminalApi, 'hosted'>> {
const hosted: HostedSessionApi = {
@@ -893,7 +894,8 @@ export function buildHostedApi(client: RpcClient): Required<Pick<NodeTerminalApi
approve: (pendingId, role) => client.request(IPC.relayHostedApprove, pendingId, role) as Promise<boolean>,
deny: (pendingId) => client.request(IPC.relayHostedDeny, pendingId) as Promise<boolean>,
onPeerPending: (listener) => client.subscribe(IPC.relayHostedPeerPending, listener as Listener),
- onPendingClosed: (listener) => client.subscribe(IPC.relayHostedPendingClosed, listener as Listener)
+ onPendingClosed: (listener) => client.subscribe(IPC.relayHostedPendingClosed, listener as Listener),
+ onSharedChanged: (listener) => client.subscribe(IPC.relayHostedSharedChanged, listener as Listener)
}
return { hosted }
}
diff --git a/src/renderer/lib/hostedOwner.test.ts b/src/renderer/lib/hostedOwner.test.ts
index 1e3d8723d..ae8db538e 100644
--- a/src/renderer/lib/hostedOwner.test.ts
+++ b/src/renderer/lib/hostedOwner.test.ts
@@ -28,7 +28,8 @@ function fakeHosted(pulled: HostedPending[] | Error = []) {
calls.push('sub:closed')
onClosed = l
return unClosed
- })
+ }),
+ onSharedChanged: () => () => {}
} as unknown as HostedSessionApi
return { hosted, calls, unPending, unClosed, push: (p: HostedPending) => onPending?.(p), close: (id: string, reason: HostedPendingClosedReason) => onClosed?.({ pendingId: id, reason }) }
}
diff --git a/src/renderer/session/relay-tab.test.ts b/src/renderer/session/relay-tab.test.ts
index c02c89229..16f794f92 100644
--- a/src/renderer/session/relay-tab.test.ts
+++ b/src/renderer/session/relay-tab.test.ts
@@ -380,7 +380,8 @@ function fakeHostedApi(self: HostedSelf | Error, pulled: HostedPending[] = []) {
onPendingClosed: vi.fn(() => {
log.push('sub:closed')
return unClosed
- })
+ }),
+ onSharedChanged: () => () => {}
}
} as unknown as NodeTerminalApi
return { api, log, unPending, unClosed, push: (p: HostedPending) => pushPending?.(p) }
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index 5f38439bf..754d22c3e 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -694,6 +694,10 @@ export const IPC = {
relayHostedDeny: 'relay:hosted:deny',
relayHostedInviteCode: 'relay:hosted:invite-code',
relayHostedSelf: 'relay:hosted:self',
+ // Sent to EVERY connected member (viewers too) after `team share`/`unshare` lands: the team's
+ // whole shared set, so a joiner opens a tab for a newly shared project and closes the tab of one
+ // that stopped being shared — with no new code and no new approval.
+ relayHostedSharedChanged: 'relay:hosted:shared-changed',
// The hosted teams THIS desktop has joined (src/main/remote/relay-bookmarks.ts). Unlike the
// hosted verbs above, these two never ride the relay: they are raw `ipcMain` handlers in the
// desktop main process, invisible to any relay peer. `relayHostedBookmarks` () lists them without
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 7b117fc41..90690d514 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -3977,6 +3977,8 @@ export interface HostedSessionApi {
deny(pendingId: string): Promise<boolean>
onPeerPending(listener: (p: HostedPending) => void): () => void
onPendingClosed(listener: (p: { pendingId: string; reason: HostedPendingClosedReason }) => void): () => void
+ /** The team's shared projects changed (share or unshare on the host): their whole set. */
+ onSharedChanged(listener: (p: { projectIds: string[] }) => void): () => void
}
/** A paired device as exposed to the renderer — the bearer token is never included. */