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(relay): accept large roster replies from authenticated services (#999)

Deee54fbffb8f44
diff --git a/cli/e2e/gatewayProcess.e2e.ts b/cli/e2e/gatewayProcess.e2e.ts
index c87bea9ab..05c21a0e8 100644
--- a/cli/e2e/gatewayProcess.e2e.ts
+++ b/cli/e2e/gatewayProcess.e2e.ts
@@ -234,6 +234,40 @@ describe('the gateway in its own process', () => {
fleet = undefined
})
+ it('keeps a remote pane connected when its agent list exceeds the desktop request limit', async () => {
+ const f = await startFleet({ envA: { HARNESSD_SERVICES: 'gateway' } })
+ fleet = f
+ await until('both machines connected to the backend', () => f.backend.nodeUp(f.a.machineId) && f.backend.nodeUp(f.b.machineId), 30_000)
+ const onB = await LocalClient.connect(f.b.daemon, { machineId: f.b.machineId })
+ clients.push(onB)
+ const agentIds: string[] = []
+ for (const index of [0, 1]) {
+ const cwd = join(f.b.daemon.projectsDir, `large-list-${index}`)
+ mkdirSync(cwd, { recursive: true })
+ const created = await onB.request('agent_create', { engine: 'claude', cwd, bypassPermission: true }, 60_000)
+ expect(created.error, JSON.stringify(created)).toBeUndefined()
+ agentIds.push(created.agent.id)
+ await until('the agent on B to bind', async () => (await row(onB, created.agent.id))?.sessionId || null, 45_000, 500)
+ // Each update fits the desktop request limit; the combined reply does
+ // not, as a real remote machine's larger agent roster did.
+ expect((await onB.request('agent_update', { agentId: created.agent.id, name: `${index}-${'a'.repeat(300 * 1024)}` })).error).toBeUndefined()
+ }
+ const before = connections(f.a.daemon)
+ const viaA = await LocalClient.connect(f.a.daemon, { machineId: f.b.machineId })
+ clients.push(viaA)
+ for (let read = 0; read < 3; read++) {
+ const list = await viaA.request<{ agents: Array<{ id: string }> }>('agents_list', {}, 10_000)
+ expect(Buffer.byteLength(JSON.stringify(list))).toBeGreaterThan(512 * 1024)
+ expect(list.agents.map((agent) => agent.id)).toEqual(expect.arrayContaining(agentIds))
+ }
+ const ended = viaA.next(isTurn('turn_ended', agentIds[0]), 30_000, 'a turn after the large agent list')
+ viaA.send('message', { agentId: agentIds[0], content: 'still connected after the large roster' })
+ await ended
+ expect(viaA.closed).toBe(false)
+ expect(connections(f.a.daemon)).toBe(before)
+ expect(f.a.daemon.coresStarted()).toBe(1)
+ })
+
it('reaches B through A\'s gateway, sealed by it, and opens again once A\'s gateway is back', async () => {
const f = await startFleet({ envA: { HARNESSD_SERVICES: 'gateway', HARNESSD_SERVICE_INITIAL_BACKOFF_MS: '200', HARNESSD_SERVICE_MAX_BACKOFF_MS: '1000' } })
fleet = f
diff --git a/cli/src/localWsServer.spec.ts b/cli/src/localWsServer.spec.ts
index 8c6c3ec31..0c9865c6e 100644
--- a/cli/src/localWsServer.spec.ts
+++ b/cli/src/localWsServer.spec.ts
@@ -1213,6 +1213,46 @@ describe('local CLI WebSocket', () => {
expect(accepted).toEqual(['search'])
})
+ it('carries a large remote agent list on an authenticated service link without dropping it', async () => {
+ const receive = vi.fn(), closed = vi.fn()
+ const url = await start(new FakeBackend(), { services: {
+ accept: (_service, token, _sink, _close, welcome) => {
+ if (token !== 'boot-token') return null
+ welcome()
+ return { receive, receiveBinary: vi.fn(), closed }
+ },
+ } })
+ const ws = new WebSocket(url)
+ await onceOpen(ws)
+ const connected = onceMessage(ws)
+ ws.send(JSON.stringify({ type: 'machine_select', payload: {
+ machineId, localProtocolVersion: 1, role: 'service', service: 'gateway', token: 'boot-token',
+ } }))
+ await connected
+ const frame = { type: 'service_notice', payload: { kind: 'windowFrame', id: 'remote-window',
+ frame: { type: 'agents_list_result', payload: { agents: [{ id: 'remote-agent', name: 'a'.repeat(1024 * 1024) }] } },
+ } }
+ ws.send(JSON.stringify(frame))
+ await vi.waitFor(() => expect(receive).toHaveBeenCalledWith(frame))
+ ws.send(JSON.stringify({ type: 'service_notice', payload: { kind: 'status', connected: true } }))
+ await vi.waitFor(() => expect(receive).toHaveBeenCalledTimes(2))
+ expect(closed).not.toHaveBeenCalled()
+ ws.close()
+ })
+
+ it('keeps the smaller JSON limit for desktop clients even if a frame claims to be a service', async () => {
+ const backend = new FakeBackend()
+ const ws = new WebSocket(await start(backend))
+ await onceOpen(ws)
+ const connected = onceMessage(ws)
+ ws.send(JSON.stringify({ type: 'machine_select', payload: { machineId, localProtocolVersion: 1 } }))
+ await connected
+ const closed = new Promise<number>((resolve) => ws.once('close', resolve))
+ ws.send(JSON.stringify({ type: 'service_notice', payload: { role: 'service', data: 'a'.repeat(512 * 1024) } }))
+ await expect(closed).resolves.toBe(4400)
+ expect(backend.frames).toEqual([])
+ })
+
it('rejects browser origins and machine-id mismatches', async () => {
const url = await start(new FakeBackend())
const browser = new WebSocket(url, { origin: 'https://example.com' })
diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.ts
index 24476b0b2..43eb34926 100644
--- a/cli/src/localWsServer.ts
+++ b/cli/src/localWsServer.ts
@@ -197,7 +197,7 @@ function rejectUpgrade(socket: Socket, status: number, reason: string): void {
)
}
-function jsonFrame(raw: RawData): Frame | null {
+function jsonFrame(raw: RawData, maxBytes = MAX_JSON_BYTES): Frame | null {
const bytes = Buffer.isBuffer(raw)
? raw
: raw instanceof ArrayBuffer
@@ -205,7 +205,7 @@ function jsonFrame(raw: RawData): Frame | null {
: Array.isArray(raw)
? Buffer.concat(raw)
: Buffer.alloc(0)
- if (!bytes.length || bytes.length > MAX_JSON_BYTES) return null
+ if (!bytes.length || bytes.length > maxBytes) return null
try {
const value = JSON.parse(bytes.toString('utf8')) as unknown
if (!value || typeof value !== 'object' || Array.isArray(value)) return null
@@ -477,7 +477,10 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
if (!selected) return selectMachine(raw, isBinary)
if (serviceLink) {
if (isBinary) { serviceLink.receiveBinary(binaryBytes(raw)); return }
- const frame = jsonFrame(raw)
+ // Tropic's agent list exceeded the window's 512 KiB limit and kept
+ // dropping the gateway, taking every remote pane with it. Only a
+ // token-authenticated service may use the existing transport ceiling.
+ const frame = jsonFrame(raw, MAX_WS_MESSAGE_BYTES)
if (!frame) { close(4400, 'invalid json frame'); return }
serviceLink.receive(frame)
return