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.

Merge pull request #464 from autonomous-ai/pr-464

0xdiego258d5066d14b8c
diff --cc cli/src/cable/cableFleet.spec.ts
index a94c4c4e8,222cda444..656292a35
--- a/cli/src/cable/cableFleet.spec.ts
+++ b/cli/src/cable/cableFleet.spec.ts
@@@ -217,30 -219,143 +219,166 @@@ describe('USB dial fleet', () =>
})
})
+ describe('a board that is not a dial', () => {
+ /**
+ * Found on a desk with a second ESP32-S3 plugged in for its own work: it shares the dial's USB ids, so
+ * the daemon opened it, heard nothing it understood, waited a minute and opened it again, for as long
+ * as it ran. That corrupted an esptool flash read twice. A board is looked at once per attachment.
+ */
+ const scenario = (opts: { inUse?: (path: string) => Promise<boolean>; file?: string } = {}) => {
+ const f = fixture()
+ const foreign = { ...device('/dev/other', 'CC:03'), session: '100' }
+ const dial = { ...device('/dev/tim', 'AA:01'), session: '200' }
+ f.setPorts([foreign, dial])
+ const options = { ...f.options, verdicts: new DialVerdicts(opts.file), watchEveryMs: 0, ...(opts.inUse ? { inUse: opts.inUse } : { inUse: async () => false }) }
+ const fleet = new CableFleet(CableSession, f.host, f.logs, DialLog, options)
+ const opens = () => f.options.open.mock.calls.map(c => c[0])
+ const scan = async () => { await (fleet as unknown as { scan(): Promise<void> }).scan(); await new Promise(r => setTimeout(r, 5)) }
+ return { f, fleet, foreign, dial, options, opens, scan }
+ }
+
+ it('is probed once, then left alone, while the dial beside it carries on', async () => {
+ const s = scenario()
+ try {
+ s.fleet.start()
+ await vi.waitFor(() => expect(s.f.ports).toHaveLength(2))
+ const other = s.f.ports.find(p => p.path === '/dev/other')!
+ const tim = s.f.ports.find(p => p.path === '/dev/tim')!
+ other.say({ t: 'hello', product: 'grid', mac: 'other', fw: '1', proto: 1 })
+ await s.f.greet([tim])
+ await vi.waitFor(() => expect(other.isOpen).toBe(false))
+ const after = s.opens().filter(p => p === '/dev/other').length
+ for (let i = 0; i < 4; i++) await s.scan()
+ expect(s.opens().filter(p => p === '/dev/other').length).toBe(after)
+ expect(tim.isOpen).toBe(true)
+ expect(s.f.host.log).toHaveBeenCalledWith(expect.stringContaining('leaving it alone until it is unplugged or reset'))
+ } finally { await s.fleet.stop() }
+ })
+
+ it('is looked at again once it has been unplugged and plugged back in', async () => {
+ const s = scenario()
+ try {
+ s.fleet.start()
+ await vi.waitFor(() => expect(s.f.ports).toHaveLength(2))
+ s.f.ports.find(p => p.path === '/dev/other')!.say({ t: 'hello', product: 'grid', mac: 'other', fw: '1', proto: 1 })
+ await vi.waitFor(() => expect(s.f.ports.find(p => p.path === '/dev/other')!.isOpen).toBe(false))
+ const before = s.opens().filter(p => p === '/dev/other').length
+ // Unplugged and plugged back in: same board, new attachment.
+ s.f.setPorts([{ ...s.foreign, session: '101' }, s.dial])
+ // Scanning as the interval would: the old entry leaves on a timer of its own, so the first scan
+ // may still find it there and the next one starts the new attachment.
+ await vi.waitFor(async () => {
+ await s.scan()
+ expect(s.opens().filter(p => p === '/dev/other').length).toBeGreaterThan(before)
+ }, { timeout: 5000 })
+ } finally { await s.fleet.stop() }
+ })
+
+ it('is looked at again once a program has worked on it and let go, which is what a flash looks like', async () => {
+ // A reset or a reflash over USB-Serial/JTAG leaves the attachment as it was (measured with esptool on
+ // the desk), so the USB view cannot say a board became a dial. Somebody holding its port for a while,
+ // and then not, can: that is esptool or idf.py. The board is looked at once more when they let go.
+ let busy = false
+ const s = scenario({ inUse: async path => path === '/dev/other' && busy })
+ try {
+ s.fleet.start()
+ await vi.waitFor(() => expect(s.f.ports).toHaveLength(2))
+ s.f.ports.find(p => p.path === '/dev/other')!.say({ t: 'hello', product: 'grid', mac: 'other', fw: '1', proto: 1 })
+ await vi.waitFor(() => expect(s.f.ports.find(p => p.path === '/dev/other')!.isOpen).toBe(false))
+ const before = s.opens().filter(p => p === '/dev/other').length
+
+ // Still ruled out while nothing has touched it, and while it is being worked on.
+ for (let i = 0; i < 3; i++) await s.scan()
+ busy = true
+ for (let i = 0; i < 3; i++) await s.scan()
+ expect(s.opens().filter(p => p === '/dev/other').length).toBe(before)
+
+ // They let go: it may be a different board now.
+ busy = false
+ await vi.waitFor(async () => {
+ await s.scan()
+ expect(s.opens().filter(p => p === '/dev/other').length).toBeGreaterThan(before)
+ }, { timeout: 5000 })
+ expect(s.f.host.log).toHaveBeenCalledWith(expect.stringContaining('was in use and is free again'))
+ } finally { await s.fleet.stop() }
+ })
+
+ it('is not probed again by the next daemon either', async () => {
+ const file = join(mkdtempSync(join(tmpdir(), 'fleet-verdicts-')), 'dial-ports.json')
+ const first = scenario({ file })
+ try {
+ first.fleet.start()
+ await vi.waitFor(() => expect(first.f.ports).toHaveLength(2))
+ first.f.ports.find(p => p.path === '/dev/other')!.say({ t: 'hello', product: 'grid', mac: 'other', fw: '1', proto: 1 })
+ await vi.waitFor(() => expect(first.f.ports.find(p => p.path === '/dev/other')!.isOpen).toBe(false))
+ } finally { await first.fleet.stop() }
+
+ const second = scenario({ file })
+ try {
+ second.fleet.start()
+ await vi.waitFor(() => expect(second.opens()).toContain('/dev/tim'))
+ for (let i = 0; i < 3; i++) await second.scan()
+ expect(second.opens()).not.toContain('/dev/other')
+ } finally { await second.fleet.stop() }
+ })
+
+ it('is never opened while another program has it, and only then once that program is done', async () => {
+ let busy = true
+ const s = scenario({ inUse: async path => path === '/dev/other' && busy })
+ try {
+ s.fleet.start()
+ await vi.waitFor(() => expect(s.opens()).toContain('/dev/tim'))
+ for (let i = 0; i < 3; i++) await s.scan()
+ expect(s.opens()).not.toContain('/dev/other')
+ expect(s.f.host.log).toHaveBeenCalledWith(expect.stringContaining('/dev/other is in use by another program'))
+ busy = false
+ await s.scan()
+ await vi.waitFor(() => expect(s.opens()).toContain('/dev/other'))
+ } finally { await s.fleet.stop() }
+ })
+
+ it('reads a port as held when a process other than this one has it open', async () => {
+ // Skipped without lsof; the fleet treats "cannot tell" as free, so there is nothing to prove there.
+ const file = join(mkdtempSync(join(tmpdir(), 'held-')), 'tty')
+ writeFileSync(file, '')
+ const holder = spawn('tail', ['-f', file], { stdio: 'ignore' })
+ try {
+ await new Promise(r => setTimeout(r, 300))
+ const held = await portInUse(file)
+ const lsofExists = await new Promise<boolean>(r => spawn('lsof', ['-v'], { stdio: 'ignore' }).on('error', () => r(false)).on('exit', () => r(true)))
+ if (lsofExists) expect(held).toBe(true)
+ } finally {
+ holder.kill()
+ await new Promise(r => holder.on('exit', r))
+ }
+ expect(await portInUse(file)).toBe(false)
+ })
+ })
+
describe('macOS USB inventory', () => {
+ it('reads multiple USB-rooted subtrees without leaking IDs into their siblings', () => {
+ const dump = `+-o Tim <class IOUSBHostDevice>
+ "idVendor" = 12346
+ "idProduct" = 4097
+ "USB Serial Number" = "AA:01"
+ +-o CDC
+ +-o serial
+ "IOCalloutDevice" = "/dev/tim"
++-o unrelated <class IOUSBHostDevice>
+ "idVendor" = 12346
+ "idProduct" = 12
+ +-o serial
+ "IOCalloutDevice" = "/dev/unrelated"
++-o Tux <class IOUSBHostDevice>
+ "USB Serial Number" = "BB:02"
+ "idProduct" = 4097
+ "idVendor" = 12346
+ +-o serial
+ "IOCalloutDevice" = "/dev/tux"`
+ expect(parseDarwinDialPorts(dump)).toEqual([device('/dev/tim', 'AA:01'), device('/dev/tux', 'BB:02')])
+ expect(parseDarwinDialPorts('')).toEqual([])
+ })
+
it('associates each tty with its own matching USB parent and serial in either property order', () => {
const dump = `+-o root
+-o Tim