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(platform): CorePlatform.clientIds() exposes the attached-client registry

enes23b95bfcd07d
diff --git a/src/core/platform-fake.ts b/src/core/platform-fake.ts
index 55bc46b3f..80b65612b 100644
--- a/src/core/platform-fake.ts
+++ b/src/core/platform-fake.ts
@@ -6,6 +6,8 @@ export interface FakePlatform extends CorePlatform {
senderListeners: Record<string, (senderId: number, ...args: any[]) => void>
sent: Array<{ to: number | 'broadcast'; channel: string; args: any[] }>
opened: string[]
+ /** Attached UI ids returned by clientIds() — tests push/splice this directly. */
+ clients: number[]
}
/** In-memory CorePlatform for tests. Not a mock library — plain recording object. */
@@ -19,6 +21,7 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
senderListeners: {},
sent: [],
opened: [],
+ clients: [],
handle(ch, fn) {
f.handlers[ch] = fn
},
@@ -37,6 +40,7 @@ export function fakePlatform(overrides: Partial<CorePlatform> = {}): FakePlatfor
broadcast(channel, ...args) {
f.sent.push({ to: 'broadcast', channel, args })
},
+ clientIds: () => f.clients,
async openExternal(url) {
f.opened.push(url)
},
diff --git a/src/core/platform.test.ts b/src/core/platform.test.ts
index db1cc5214..b391fb5d3 100644
--- a/src/core/platform.test.ts
+++ b/src/core/platform.test.ts
@@ -29,6 +29,13 @@ describe('core platform accessor', () => {
expect(fake.opened).toEqual(['https://example.com'])
})
+ it('fake clientIds reflects the attached-client list tests set up', () => {
+ const fake = fakePlatform()
+ expect(fake.clientIds()).toEqual([])
+ fake.clients.push(1, 2)
+ expect(fake.clientIds()).toEqual([1, 2])
+ })
+
it('fake records onWithSender listeners and passes the sender id through', () => {
const fake = fakePlatform()
const seen: Array<[number, string]> = []
diff --git a/src/core/platform.ts b/src/core/platform.ts
index 4f46715b8..35824b1c9 100644
--- a/src/core/platform.ts
+++ b/src/core/platform.ts
@@ -21,6 +21,10 @@ export interface CorePlatform {
sendTo(uiId: number, channel: string, ...args: any[]): void
/** Send to every attached UI (Electron: the main window). */
broadcast(channel: string, ...args: any[]): void
+ /** Ids of every attached UI, in attach order (Server: each authenticated WS connection;
+ * Electron: the main window, or none while it is closed). Lets a service address "everyone
+ * EXCEPT the sender", which broadcast() cannot express — see src/core/canvas-sync.ts. */
+ clientIds(): number[]
/** Open a URL in the user's default browser. */
openExternal(url: string): Promise<void>
}
diff --git a/src/main/main-window.test.ts b/src/main/main-window.test.ts
index f2c62bfc8..49aa146df 100644
--- a/src/main/main-window.test.ts
+++ b/src/main/main-window.test.ts
@@ -1,5 +1,12 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
-import { setMainWindow, getMainWindow, sendToMain, shouldHideOnClose, type MainWindowLike } from './main-window'
+import {
+ setMainWindow,
+ getMainWindow,
+ sendToMain,
+ mainWindowClientIds,
+ shouldHideOnClose,
+ type MainWindowLike
+} from './main-window'
function fakeWindow(): MainWindowLike & {
sent: [string, ...unknown[]][]
@@ -21,6 +28,7 @@ function fakeWindow(): MainWindowLike & {
if (event === 'closed') closedListeners.push(cb)
},
webContents: {
+ id: undefined as number | undefined,
send: (channel: string, ...args: unknown[]) => {
sent.push([channel, ...args])
}
@@ -85,6 +93,15 @@ describe('main-window tracking', () => {
expect(getMainWindow()).toBe(second)
})
+ it('mainWindowClientIds returns the live window webContents id, or [] when there is none', () => {
+ const w = fakeWindow()
+ w.webContents.id = 7
+ setMainWindow(w)
+ expect(mainWindowClientIds()).toEqual([7])
+ w.destroy()
+ expect(mainWindowClientIds()).toEqual([])
+ })
+
it('getMainWindow returns null when nothing was registered or after closed', () => {
const w = fakeWindow()
setMainWindow(w)
diff --git a/src/main/main-window.ts b/src/main/main-window.ts
index 0759e3abc..ba8518667 100644
--- a/src/main/main-window.ts
+++ b/src/main/main-window.ts
@@ -14,7 +14,9 @@ export interface MainWindowLike {
show(): void
focus(): void
on(event: 'closed', cb: () => void): void
- webContents: { send(channel: string, ...args: unknown[]): void }
+ // `id` is Electron's webContents id — the same number CorePlatform addresses a UI by
+ // (sendTo / the sender id of an ipcMain event). Optional so a test double may omit it.
+ webContents: { id?: number; send(channel: string, ...args: unknown[]): void }
}
let current: MainWindowLike | null = null
@@ -35,6 +37,14 @@ export function sendToMain(channel: string, ...args: unknown[]): void {
getMainWindow()?.webContents.send(channel, ...args)
}
+/** The attached renderer client ids — Electron has exactly one (the main window's webContents),
+ * or none while the window is closed on macOS. Resolved AT CALL TIME, like sendToMain, so a
+ * recreated window is picked up. Feeds CorePlatform.clientIds(). */
+export function mainWindowClientIds(): number[] {
+ const id = getMainWindow()?.webContents.id
+ return typeof id === 'number' ? [id] : []
+}
+
// macOS convention: closing the window hides it (the app — and its tmux sessions,
// hook server, updater, license watchers — keeps running); a real close only happens
// on quit. Other platforms quit on window close, so never intercept there.
diff --git a/src/main/platform-electron.test.ts b/src/main/platform-electron.test.ts
index a76c9bbb7..ed072059c 100644
--- a/src/main/platform-electron.test.ts
+++ b/src/main/platform-electron.test.ts
@@ -3,7 +3,8 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
const h: {
handlers: Record<string, (...a: any[]) => unknown>
sent: Array<{ id?: number; channel: string; args: any[] }>
-} = { handlers: {}, sent: [] }
+ clientIds: number[]
+} = { handlers: {}, sent: [], clientIds: [] }
vi.mock('electron', () => ({
app: {
@@ -30,6 +31,7 @@ vi.mock('electron', () => ({
vi.mock('./main-window', () => ({
sendToMain: (ch: string, ...args: any[]) => h.sent.push({ channel: ch, args }),
+ mainWindowClientIds: () => h.clientIds,
}))
import { electronPlatform } from './platform-electron'
@@ -37,6 +39,7 @@ import { electronPlatform } from './platform-electron'
beforeEach(() => {
h.handlers = {}
h.sent = []
+ h.clientIds = []
})
describe('electronPlatform', () => {
@@ -55,6 +58,13 @@ describe('electronPlatform', () => {
expect(await h.handlers['c2']({ sender: { id: 7 } }, 'x')).toBe('7:x')
})
+ it('clientIds reports the live main window (empty while there is no window)', () => {
+ const p = electronPlatform()
+ expect(p.clientIds()).toEqual([])
+ h.clientIds = [5]
+ expect(p.clientIds()).toEqual([5])
+ })
+
it('sendTo drops silently when the webContents is gone', () => {
const p = electronPlatform()
p.sendTo(1, 'ev', 'a')
diff --git a/src/main/platform-electron.ts b/src/main/platform-electron.ts
index 547ba6b03..c3c28ecde 100644
--- a/src/main/platform-electron.ts
+++ b/src/main/platform-electron.ts
@@ -1,6 +1,6 @@
import { app, ipcMain, shell, webContents } from 'electron'
import type { CorePlatform } from '../core/platform'
-import { sendToMain } from './main-window'
+import { mainWindowClientIds, sendToMain } from './main-window'
/** The Electron shell's CorePlatform. Getters keep app.getPath lazy (safe pre-ready). */
export function electronPlatform(): CorePlatform {
@@ -23,6 +23,7 @@ export function electronPlatform(): CorePlatform {
if (wc && !wc.isDestroyed()) wc.send(ch, ...args)
},
broadcast: (ch, ...args) => sendToMain(ch, ...args),
+ clientIds: () => mainWindowClientIds(),
openExternal: (url) => shell.openExternal(url),
}
}
diff --git a/src/server/platform-server.test.ts b/src/server/platform-server.test.ts
index 96cc3caf8..614282a71 100644
--- a/src/server/platform-server.test.ts
+++ b/src/server/platform-server.test.ts
@@ -136,6 +136,16 @@ describe('ServerPlatform', () => {
expect(flow).toEqual([]) // ui2 owes nothing → no resume fired
})
+ it('clientIds lists every attached sink and drops detached ones', () => {
+ const p = new ServerPlatform({ userDataDir: '/tmp/x', appVersion: '1.0.0' })
+ expect(p.clientIds()).toEqual([])
+ const a = p.attach(fakeSink().sink)
+ const b = p.attach(fakeSink().sink)
+ expect(p.clientIds()).toEqual([a, b])
+ p.detach(a)
+ expect(p.clientIds()).toEqual([b])
+ })
+
it('exposes userDataDir/appVersion/isPackaged; openExternal rejects', async () => {
const p = new ServerPlatform({ userDataDir: '/data', appVersion: '9.9.9' })
expect(p.userDataDir).toBe('/data')
diff --git a/src/server/platform-server.ts b/src/server/platform-server.ts
index c92b33f6c..f4561c50c 100644
--- a/src/server/platform-server.ts
+++ b/src/server/platform-server.ts
@@ -388,6 +388,11 @@ export class ServerPlatform implements CorePlatform {
for (const uiId of this.sinks.keys()) this.sendTo(uiId, channel, ...args)
}
+ /** Every attached connection, in attach order (Map preserves insertion order; detach removes). */
+ clientIds(): number[] {
+ return [...this.sinks.keys()]
+ }
+
openExternal(_url: string): Promise<void> {
return Promise.reject(new Error('openExternal is not available on a headless server'))
}