Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(device): retain the desktop pane roster when a connection closes (#996)

Deee3d16af159dc
diff --git a/cli/e2e/devicesProcess.e2e.ts b/cli/e2e/devicesProcess.e2e.ts
index 3a6e8a443..e9ba0fdf1 100644
--- a/cli/e2e/devicesProcess.e2e.ts
+++ b/cli/e2e/devicesProcess.e2e.ts
@@ -106,6 +106,35 @@ describe('the devices in their own process', () => {
window.close()
})
+ it('keeps the selected pane on the dial when another desktop connection closes', async () => {
+ const { d, desk } = await fresh()
+ const window = await LocalClient.connect(d)
+ const otherConnection = await LocalClient.connect(d)
+ const agentId = await create(d, window, 'devices-desk-connections')
+ onTab(window, [agentId])
+ onTab(otherConnection, [agentId])
+ const dial = await desk.plug('E2E-A', 'e2:e0:00:00:00:0a')
+ await heard(dial, 'agents.end')
+ let since = dial.messages.length
+ window.send('app_focus', { agentId })
+ await heard(dial, 'focus', since, agentId)
+ since = dial.messages.length
+ otherConnection.close()
+ await until('the extra connection to close', () => otherConnection.closed || null, 5000, 50)
+ // A forced list read passes behind connection cleanup and proves the
+ // devices process still holds the surviving window's tab and selection.
+ dial.send({ t: 'agents.list' })
+ const roster = await heard(dial, 'agents.end', since)
+ expect(roster.tab).toBe('t1')
+ expect(dial.messages.slice(since).filter(m => m.t === 'agent').map(m => m.id)).toEqual([agentId])
+ expect(dial.messages.slice(since).filter(m => m.t === 'agents.end').every(m => m.tab === 't1')).toBe(true)
+ await heard(dial, 'focus', since, agentId)
+ window.close()
+ since = dial.messages.length
+ await heard(dial, 'agents.end', since)
+ await until('the last window to clear the dial', () => dial.messages.slice(since).some(m => m.t === 'agents.end' && m.tab === '') || null, 5000, 50)
+ })
+
it('killed outright: agents and windows go on, ⌘K says so at once, and the dial comes back to its question and its working tile', async () => {
const { d, desk } = await fresh()
const window = await LocalClient.connect(d)
diff --git a/cli/src/localWsServer.spec.ts b/cli/src/localWsServer.spec.ts
index ea26ab5ae..8c6c3ec31 100644
--- a/cli/src/localWsServer.spec.ts
+++ b/cli/src/localWsServer.spec.ts
@@ -676,6 +676,57 @@ describe('local CLI WebSocket', () => {
expect(tabs).toHaveBeenLastCalledWith(backend.connId, null)
})
+ for (const latestCloses of [false, true]) {
+ it(`keeps the device's desk when a ${latestCloses ? 'latest' : 'background'} machine connection closes`, async () => {
+ const backend = new FakeBackend(), panes = vi.fn(), swarms = vi.fn(), tabs = vi.fn()
+ const url = await start(backend, {
+ onAppPanes: panes, onAppSwarms: swarms, onAppTabAgents: tabs,
+ relayPool: {
+ acquire: async (_machine, _env, _select, sink) => {
+ sink.sendFrame({ type: 'connected', payload: { machineId: 'remote' } })
+ return { send: async () => {}, sendBinary: async () => {}, detach: () => {} }
+ },
+ acquireIsolated: vi.fn(),
+ invalidate: () => {}, invalidateIsolated: () => {},
+ } as NonNullable<LocalWsServerOptions['relayPool']>,
+ autonomousEnv: 'prod',
+ })
+ const connect = async (selected: string) => {
+ const ws = new WebSocket(url)
+ await onceOpen(ws)
+ const ready = onceMessage(ws)
+ ws.send(JSON.stringify({ type: 'machine_select', payload: { machineId: selected, localProtocolVersion: 1 } }))
+ await ready
+ return ws
+ }
+ const first = await connect(machineId), second = await connect('remote')
+ const roster = { active: 'tab', swarms: [{ id: 'tab', name: 'Work', agentIds: ['a1'], panes: 1 }], tiles: [] }
+ const announce = async (ws: WebSocket, foreground: boolean) => {
+ const count = panes.mock.calls.length
+ ws.send(JSON.stringify({ type: 'app_panes', payload: { agentIds: ['a1'], foreground } }))
+ ws.send(JSON.stringify({ type: 'app_swarms', payload: roster }))
+ await vi.waitFor(() => {
+ expect(panes).toHaveBeenCalledTimes(count + 1)
+ expect(swarms).toHaveBeenCalledTimes(count + 1)
+ })
+ }
+ await announce(first, false)
+ await announce(second, true)
+ // Reannouncing on an existing socket makes it the newest reporter too.
+ await announce(first, false)
+ const closing = latestCloses ? first : second, surviving = latestCloses ? second : first
+ closing.close()
+ await vi.waitFor(() => expect(tabs).toHaveBeenCalledTimes(4))
+ expect(panes).toHaveBeenLastCalledWith(['a1'], latestCloses)
+ expect(swarms).toHaveBeenLastCalledWith(roster)
+ expect(panes.mock.calls.some(([ids]) => ids.length === 0)).toBe(false)
+ expect(swarms.mock.calls.some(([rows]) => rows === null)).toBe(false)
+ surviving.close()
+ await vi.waitFor(() => expect(panes).toHaveBeenLastCalledWith([], false))
+ expect(swarms).toHaveBeenLastCalledWith(null)
+ })
+ }
+
it('follows an explicit app_focus, and keeps it off the wire', async () => {
const backend = new FakeBackend()
const moves: Array<{ machineId: string; agentId: string }> = []
diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.ts
index e7e30f978..24476b0b2 100644
--- a/cli/src/localWsServer.ts
+++ b/cli/src/localWsServer.ts
@@ -298,6 +298,11 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
// background terminal attachments on ANY of those connections must not overwrite that selection.
const explicitFocusClients = new Set<string>()
const windowSinks = new Map<string, LocalClientSink>()
+ // The desktop reports the same desk on one connection per machine. Losing a
+ // remote relay must not erase the desk while another connection still holds it.
+ // Map order records the latest announcement, including a replacement socket.
+ const paneRosters = new Map<string, { ids: string[]; foreground: boolean }>()
+ const swarmRosters = new Map<string, AppSwarms>()
const onUpgrade = (req: http.IncomingMessage, socket: Socket, head: Buffer): void => {
const path = (req.url ?? '').split('?')[0]
@@ -527,6 +532,8 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
// announcing work that is in plain sight.
const foreground = payload?.foreground !== false
sentPanes = true
+ paneRosters.delete(connId)
+ paneRosters.set(connId, { ids, foreground })
options.onAppPanes(ids, foreground)
return
}
@@ -581,6 +588,8 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
const swarms = appSwarmsFrom(parsed.payload)
if (swarms) {
sentSwarms = true
+ swarmRosters.delete(connId)
+ swarmRosters.set(connId, swarms)
options.onAppSwarms(swarms)
options.onAppTabAgents?.(connId, swarms.swarms.flatMap(s => s.agentIds))
}
@@ -778,11 +787,22 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
const wasWindow = windowSinks.delete(connId)
explicitFocusClients.delete(connId)
if (wasWindow && boundMachineId) options.onAppDisconnect?.(boundMachineId, connId)
- // A window that went away has no tiles open. Left standing, the roster
- // would keep silencing the dial for agents nobody can see any more —
- // exactly backwards, and permanently.
- if (sentPanes) options.onAppPanes?.([], false)
- if (sentSwarms) options.onAppSwarms?.(null)
+ // Only the latest reporter can change the desk on close. If another
+ // socket still reports a window, restore its latest state; the last
+ // window going away is what empties the device's panes and tabs.
+ if (sentPanes) {
+ const latest = [...paneRosters.keys()].at(-1) === connId
+ paneRosters.delete(connId)
+ if (latest) {
+ const remaining = [...paneRosters.values()].at(-1)
+ options.onAppPanes?.(remaining?.ids ?? [], remaining?.foreground ?? false)
+ }
+ }
+ if (sentSwarms) {
+ const latest = [...swarmRosters.keys()].at(-1) === connId
+ swarmRosters.delete(connId)
+ if (latest) options.onAppSwarms?.([...swarmRosters.values()].at(-1) ?? null)
+ }
if (sentSwarms) options.onAppTabAgents?.(connId, null)
if (serviceLink) { serviceLink.closed(); serviceLink = null }
else if (relay) { relay.detach(); relay = null }