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: reconnect idle autonomous-device direct sockets

leocdc0d4993870
diff --git a/cli/src/lib/autonomous-device/direct.spec.ts b/cli/src/lib/autonomous-device/direct.spec.ts
index 7d63b783b..b4c8f7e53 100644
--- a/cli/src/lib/autonomous-device/direct.spec.ts
+++ b/cli/src/lib/autonomous-device/direct.spec.ts
@@ -43,6 +43,22 @@ describe('direct Autonomous device endpoint selection', () => {
await expect(f.direct.pair(f.row.id, 'CODE12')).rejects.toThrow('session did not authenticate')
expect(f.direct.connected()).toBe(0)
})
+ it('terminates a silent link and reconnects, while a pinged link stays open', async () => {
+ const f = await fixture(); await f.direct.pair(f.row.id, 'CODE12'); f.direct.stop()
+ await new Promise(resolve => setTimeout(resolve, 20))
+ const sockets: import('ws').WebSocket[] = []
+ f.wss.removeAllListeners('connection'); f.wss.on('connection', ws => sockets.push(ws))
+ const quiet = new AutonomousDeviceDirect(f.host, f.dir, f.discovery, 100); clean.unshift(() => quiet.stop()); quiet.start()
+ await vi.waitFor(() => expect(sockets).toHaveLength(1))
+ await vi.waitFor(() => expect(sockets[0].readyState).toBe(sockets[0].CLOSED), { timeout: 1000 })
+ expect(quiet.connected()).toBe(0)
+ ;(quiet as unknown as { reconnect: () => Promise<void> }).reconnect()
+ await vi.waitFor(() => expect(sockets).toHaveLength(2))
+ const ping = setInterval(() => { for (const ws of sockets) if (ws.readyState === ws.OPEN) ws.ping() }, 30); clean.push(() => clearInterval(ping))
+ await new Promise(resolve => setTimeout(resolve, 400))
+ expect(sockets[1].readyState).toBe(sockets[1].OPEN)
+ expect(quiet.connected()).toBe(1)
+ })
it('refuses a reconnect identity different from the saved discovered association', async () => {
const f = await fixture(); await f.direct.pair(f.row.id, 'CODE12'); f.direct.stop()
await new Promise(resolve => setTimeout(resolve, 20)); f.receive.mockClear()
diff --git a/cli/src/lib/autonomous-device/direct.ts b/cli/src/lib/autonomous-device/direct.ts
index 790d77c74..ef82dc8c7 100644
--- a/cli/src/lib/autonomous-device/direct.ts
+++ b/cli/src/lib/autonomous-device/direct.ts
@@ -31,7 +31,8 @@ export class AutonomousDeviceDirect {
private stopping = false
private pairing = false
private readonly file: string
- constructor(private readonly host: DirectDeviceHost, private readonly dataDir: string, private readonly discovery = discoverDevices) {
+ /** The device pings every 15s; a link that hears nothing for this long is dead (Mac slept, network blackout) and must be torn down so reconnect() can replace it. */
+ constructor(private readonly host: DirectDeviceHost, private readonly dataDir: string, private readonly discovery = discoverDevices, private readonly idleMs = 60000) {
secureStateDirectory(dataDir); this.file = join(dataDir, 'autonomous-device-connections.json')
if (hardenPrivateStateFileIfPresent(this.file, 65536)) {
const value: unknown = JSON.parse(readPrivateStateFile(this.file, 65536))
@@ -99,7 +100,12 @@ export class AutonomousDeviceDirect {
const ws = new WebSocket(url, { maxPayload: 65536, handshakeTimeout: 10000 }), connId = `autonomous-direct:${randomUUID()}`
const expected = pairingAllowed ? undefined : this.associations.find(a => a.discoveryId === candidate.id)?.fingerprint
const authTimer = setTimeout(() => { if (!this.host.authenticatedFingerprint(connId)) ws.terminate() }, 20000)
- ws.once('close', () => clearTimeout(authTimer))
+ // Half-open guard: macOS keeps a dead TCP socket ESTABLISHED indefinitely (keepalive off, no client ping),
+ // so without this the link never emits 'close' and reconnect() skips the device forever.
+ let idleTimer = setTimeout(() => ws.terminate(), this.idleMs)
+ const alive = () => { clearTimeout(idleTimer); idleTimer = setTimeout(() => ws.terminate(), this.idleMs) }
+ ws.on('ping', alive); ws.on('pong', alive); ws.on('message', alive)
+ ws.once('close', () => { clearTimeout(authTimer); clearTimeout(idleTimer) })
const ready = new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => { ws.terminate(); reject(new Error('Device connection timed out')) }, 10000)
ws.once('open', () => {