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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
harnessd: the devices' process runs only once there is a device (#946)
Deea33d5a5a53bf
diff --git a/cli/e2e/devicesOnDemand.e2e.ts b/cli/e2e/devicesOnDemand.e2e.tsnew file mode 100644index 000000000..aaf3bfc1e--- /dev/null+++ b/cli/e2e/devicesOnDemand.e2e.ts@@ -0,0 +1,119 @@+/**+ * The devices' process on demand (core/devicesWake.ts, harnessd/services.ts `devices`), on the real daemon with+ * fake dials on pseudo-terminals: a computer with no device runs no devices' process, about 72 MiB it never+ * pays, and the first device or the first request for one starts it.+ * - A dial plugged in: its port is seen where the dial's own scan would look (here the suite's list, as+ * cable/cableFleet.ts reads it), the process is started, and the dial is welcomed; it stays for the next.+ * - The Devices tab and ⌘K: their first request starts it, waits for it and is answered by it.+ * - Named in `HARNESSD_SERVICES` (tests, support), it starts with the daemon, as before.+ * - A Wi-Fi device's session starts it too, and nothing it sends meanwhile is lost: e2e/wifiDevice.e2e.ts.+ */+import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs'+import { join } from 'node:path'+import { afterEach, describe, expect, it, onTestFailed } from 'vitest'+import { LocalClient } from './harness/client.js'+import { IsolatedDaemon, until } from './harness/daemon.js'+import { devicesPids, SETTINGS } from './harness/desk.js'+import { FakeDial } from './harness/fakeDial.js'++const starts = (d: IsolatedDaemon) => [...d.log().matchAll(/\[harnessd\] service devices started/g)].length+const connections = (d: IsolatedDaemon) => d.log().split('\n').filter((line) => line.includes('[services] devices connected')).length+/** ⌘K's pick for a typed task: `route_task` answers as `route_result`, under the asker's request id. */+async function routeTask(client: LocalClient, text: string): Promise<Record<string, any>> {+ const requestId = `route-${Math.random().toString(36).slice(2)}`+ const answered = client.next((frame) => frame.type === 'route_result' && frame.payload?.requestId === requestId, 40_000, 'route_result')+ client.send('route_task', { requestId, text })+ return (await answered).payload as Record<string, any>+}++describe('the devices\' process, once there is a device', () => {+ let daemon: IsolatedDaemon | undefined+ const dials: FakeDial[] = []+ afterEach(async () => {+ for (const dial of dials.splice(0)) await dial.close()+ await daemon?.close()+ daemon = undefined+ })+ const fresh = async (env: Record<string, string> = {}) => {+ const d = await IsolatedDaemon.create({ env: { CABLE_DISABLE: 'false', ...env } })+ daemon = d+ const file = join(d.root, 'dials.json')+ writeFileSync(file, '[]')+ d.env.HARNESSD_TEST_DIAL_PORT = file+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-150).join('\n')}`) })+ await d.start()+ // The other services are up, and the core has looked for a dial a few times.+ await until('the services to connect', () => d.log().includes('[services] search connected') || null, 30_000, 200)+ await new Promise((resolve) => setTimeout(resolve, 5_000))+ return { d, file }+ }+ /** A dial on the desk's list, and how long from that write to its welcome. */+ const plug = async (file: string, serial: string, mac: string): Promise<{ dial: FakeDial; welcomedMs: number }> => {+ const dial = await FakeDial.open({ mac, settings: SETTINGS })+ dials.push(dial)+ const at = Date.now()+ writeFileSync(file, JSON.stringify([{ serial, path: dial.path }]))+ dial.keepGreeting()+ const welcome = await dial.next((m) => m.t === 'welcome', 60_000, 'a welcome')+ return { dial, welcomedMs: dial.arrivedAt[dial.messages.indexOf(welcome)] - at }+ }++ it('runs no devices\' process with no device; a dial plugged in starts it and is welcomed, and it stays for the next', async () => {+ const { d, file } = await fresh()+ expect(starts(d), 'a devices\' process with no device').toBe(0)+ expect(devicesPids(d)).toEqual([])+ const window = await LocalClient.connect(d)+ // What a window says on every change goes on with no one to hear it: kept, and told once they start.+ window.send('app_panes', { agentIds: [], foreground: true })+ const first = await plug(file, 'E2E-A', 'e2:e0:00:00:00:0a')+ expect(d.log()).toContain('[devices] a dial\'s port is there: asking for the devices\' process')+ expect(starts(d)).toBe(1)+ await first.dial.next((m) => m.t === 'agents.end', 30_000, 'the agent list')+ // Unplugged and plugged in again: the same process, which a dial no longer there does not stop.+ writeFileSync(file, '[]')+ await first.dial.close()+ await new Promise((resolve) => setTimeout(resolve, 3_000))+ const again = await plug(file, 'E2E-A', 'e2:e0:00:00:00:0a')+ expect(starts(d)).toBe(1)+ expect(connections(d)).toBe(1)+ // `E2E_MEASURE=<file>`: what the pull request reports, kept. The dial greets every two seconds until+ // welcomed, and the core and the devices each look every two seconds.+ if (process.env.E2E_MEASURE) appendFileSync(process.env.E2E_MEASURE, `plug-in to welcome: first ${first.welcomedMs} ms (the process started), again ${again.welcomedMs} ms\n`)+ expect(first.welcomedMs).toBeLessThan(15_000)+ expect(d.coresStarted()).toBe(1)+ window.close()+ })++ it('the Devices tab\'s first request starts it, waits for it and is answered by it', async () => {+ const { d } = await fresh()+ const window = await LocalClient.connect(d)+ expect(starts(d)).toBe(0)+ expect(await window.request('harness_devices_list', {}, 30_000)).toMatchObject({ protocol: 1, status: { attached: false } })+ expect(starts(d)).toBe(1)+ expect(d.log()).toContain('[services] devices connected')+ window.close()+ })++ it('⌘K\'s first task starts it and is answered by the fleet\'s router, not refused', async () => {+ const { d } = await fresh()+ const window = await LocalClient.connect(d)+ const cwd = join(d.projectsDir, 'on-demand')+ mkdirSync(cwd, { recursive: true })+ const created = await window.request('agent_create', { engine: 'claude', cwd, bypassPermission: true }, 60_000)+ expect(created.error, JSON.stringify(created)).toBeUndefined()+ expect(starts(d)).toBe(0)+ const routed = await routeTask(window, 'fix the parser')+ expect(routed.reason).not.toBe('the devices service is unavailable')+ expect(starts(d)).toBe(1)+ window.close()+ })++ it('named in HARNESSD_SERVICES, it starts with the daemon, with no device', async () => {+ const d = await IsolatedDaemon.create({ env: { HARNESSD_SERVICES: 'devices' } })+ daemon = d+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-150).join('\n')}`) })+ await d.start()+ await until('the devices to connect', () => connections(d) >= 1 || null, 30_000, 200)+ expect(d.log()).not.toContain('asking for the devices\' process')+ })+})diff --git a/cli/e2e/devicesProcess.e2e.ts b/cli/e2e/devicesProcess.e2e.tsindex 2eec82b59..3a6e8a443 100644--- a/cli/e2e/devicesProcess.e2e.ts+++ b/cli/e2e/devicesProcess.e2e.ts@@ -72,7 +72,7 @@ describe('the devices in their own process', () => {const theDesk = deskonTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-200).join('\n')}`) })await d.start()- await until('the devices to connect to the core', () => connections(d) >= 1 || null, 30_000, 200)+ // No process until there is a device (core/devicesWake.ts): each test plugs its dials in, or asks for them.return { d, desk: theDesk }}/** A window with a tab holding these agents, as the desktop says it on every change. */@@ -168,9 +168,10 @@ describe('the devices in their own process', () => {})it('leaking: restarted at its memory budget, before it can hurt anything else', async () => {- const { d } = await fresh({ HARNESSD_TEST_FAULTS: 'devices.leak', HARNESSD_SERVICE_HEAP_LIMIT_MIB: '128' })+ const { d, desk } = await fresh({ HARNESSD_TEST_FAULTS: 'devices.leak', HARNESSD_SERVICE_HEAP_LIMIT_MIB: '128' })const window = await LocalClient.connect(d)const agentId = await create(d, window, 'devices-leaking')+ await desk.plug('E2E-A', 'e2:e0:00:00:00:0a')await until('the master to restart the devices for memory', () => /\[harnessd\] service devices: (its heap is at|it is using)/.test(d.log()) || null, 60_000, 250)await until('the devices to be started again', () => restarts(d) >= 1 || null, 30_000, 200)await turn(window, agentId, 'the leak was the devices\' alone')@@ -178,7 +179,7 @@ describe('the devices in their own process', () => {window.close()})- it('crashing on every start: parked, its requests say so, and the agents go on', async () => {+ it('crashing on every start: parked, its requests say so at once, and the agents go on', async () => {const d = await IsolatedDaemon.create({ env: { CABLE_DISABLE: 'false', HARNESSD_TEST_FAULTS: 'devices.crash', HARNESSD_SERVICE_PARK_CRASHES: '3',HARNESSD_SERVICE_INITIAL_BACKOFF_MS: '200', HARNESSD_SERVICE_MAX_BACKOFF_MS: '1000' } })daemon = d@@ -186,9 +187,14 @@ describe('the devices in their own process', () => {await d.start()const window = await LocalClient.connect(d)const agentId = await create(d, window, 'devices-crash-loop')+ // The Devices tab's first request starts them; whatever it is answered, it is an answer.+ await window.request('harness_devices_list', {}, 45_000)await until('the master to park the devices', () => d.log().includes('[harnessd] service devices ended 3 times') || null, 60_000, 250)+ // Down now: each request is answered at once, ⌘K's too, not after its own 25 s.+ const asked = Date.now()expect(await window.request('harness_devices_list', {}, 10_000)).toMatchObject({ error: 'SERVICE_UNAVAILABLE', service: 'devices', retryable: true })expect(await routeTask(window, 'fix the parser')).toMatchObject({ agentId: '', reason: 'the devices service is unavailable' })+ expect(Date.now() - asked).toBeLessThan(5_000)await turn(window, agentId, 'the devices are parked and nothing else cares')expect(d.coresStarted()).toBe(1)window.close()diff --git a/cli/e2e/robustness.e2e.ts b/cli/e2e/robustness.e2e.tsindex cad616d3e..05201b0d1 100644--- a/cli/e2e/robustness.e2e.ts+++ b/cli/e2e/robustness.e2e.ts@@ -194,6 +194,9 @@ describe('what the daemon survives', () => {// The handshake itself asks the dial for its status.const desktop = await LocalClient.connect(d)const agent = await boundAgent(d, desktop, 'devices-failing')+ // No device here: the Devices tab's request starts their process (core/devicesWake.ts), whatever it answers.+ await desktop.request('harness_devices_list', {}, 30_000)+ await until('the Wi-Fi device\'s service to connect', () => d.log().includes('[services] wifi connected') || null, 30_000, 100)// What a desktop sends on every pane change, focus, read and spoken reply — each one reaching the dial,// a window bridge or the WiFi device service.for (let i = 0; i < 3; i++) {@@ -232,13 +235,14 @@ describe('what the daemon survives', () => {writeFileSync(join(d.dataDir, 'autonomous-device-connections.json'), '{"not": "a list"}', { mode: 0o600 })writeFileSync(join(d.dataDir, 'device-results.json'), '{"version": 1, "entries": [', { mode: 0o600 })await d.start()- await until('the device service to be left out', () => /the Wi-Fi device service could not be started/.test(d.log()), 30_000)// The requests that need no device piece still answer; the ones that need one say it is not running,// not that it is still starting.const credential = readFileSync(join(d.dataDir, 'hook-credential'), 'utf8').trim()const device = (path: string) => fetch(`http://127.0.0.1:${d.port}/api/autonomous-device/${path}`, { headers: { authorization: `Bearer ${credential}` } })await until('the device requests to answer', async () => (await device('list')).status === 200, 30_000, 250)+ // A discovery starts the devices' process (core/devicesWake.ts), which leaves out what it cannot read.expect(await (await device('discover')).json()).toMatchObject({ error: { code: 'UNAVAILABLE', message: expect.stringContaining('is not running on this computer') } })+ await until('the device service to be left out', () => /the Wi-Fi device service could not be started/.test(d.log()), 30_000)const client = await LocalClient.connect(d)expect(Array.isArray((await client.request('agents_list', { includeStopped: true })).agents)).toBe(true)const agent = await boundAgent(d, client, 'after-the-corruption')diff --git a/cli/e2e/services.e2e.ts b/cli/e2e/services.e2e.tsindex f111bdc87..856352920 100644--- a/cli/e2e/services.e2e.ts+++ b/cli/e2e/services.e2e.ts@@ -331,7 +331,9 @@ describe('a failing service never takes the core down', () => {expect(await client.request('route_send', { agentId, text: 'sent by cmd-k' })).toMatchObject({ ok: true })expect((await started).payload?.userMessage).toBe('sent by cmd-k')await ended- expect(daemon.log()).toContain('[devices] dial failed · injected fault: dial')+ // ⌘K started the devices (core/devicesWake.ts); the window's tab, as a desktop says it, reaches the dial.+ client.send('app_panes', { agentIds: [agentId], foreground: true })+ await until('the dial to fail', () => daemon?.log().includes('[devices] dial failed · injected fault: dial') || null, 15_000, 100)expect(daemon.coresStarted()).toBe(1)client.close()})diff --git a/cli/e2e/wifiDevice.e2e.ts b/cli/e2e/wifiDevice.e2e.tsindex 8d82c25f3..80f581627 100644--- a/cli/e2e/wifiDevice.e2e.ts+++ b/cli/e2e/wifiDevice.e2e.ts@@ -10,7 +10,9 @@* - two dials and a Wi-Fi device are three devices: each hears its own, the desk's turn reaches all three,* and a dial unplugged costs the other two nothing;* - its service failing to start costs it alone: the dials and the agents go on, and- * `harness device receipt` says it is not running.+ * `harness device receipt` says it is not running;+ * - paired, it starts the devices' process with the daemon, with no dial (core/devicesWake.ts), and what it+ * sends while that process starts again is held by the core and answered.*/import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'@@ -164,6 +166,24 @@ describe('the Wi-Fi device with the dials', () => {expect(machine.daemon.coresStarted()).toBe(1)})+ it('paired, it starts the devices\' process with the daemon, and a hello it sends once while that process starts again is held and answered', async () => {+ const { machine, agentId, device } = await fresh()+ const log = () => machine.daemon.log()+ // No dial here: its pairing asks for the process (core/devicesWake.ts).+ await until('the Wi-Fi device\'s service to connect', () => wifiConnections(world!) >= 1 || null, 30_000, 200)+ expect(log()).toContain('[devices] a paired Wi-Fi device: asking for the devices\' process')+ await connect(device)+ for (const pid of devicesPids(machine.daemon)) process.kill(pid, 'SIGKILL')+ await until('the core to see the service go', () => log().includes('[services] wifi disconnected') || null, 15_000, 50)+ // One hello, no retry: held by the core while the process starts, handed on once its service is resumed.+ const hello = await device.hello()+ expect(hello).toMatchObject({ type: 'hello_result', proto: 1, machineId: machine.machineId })+ expect(wifiConnections(world!)).toBe(2)+ expect((await device.ask('agents.list')).agents).toEqual([expect.objectContaining({ agentId })])+ await deviceTurn(device, machine.machineId, agentId, 'held', 'the first prompt after the start')+ expect(machine.daemon.coresStarted()).toBe(1)+ })+it('two dials and a Wi-Fi device are three devices: each its own, the desk\'s turn on all three, and a dial unplugged costs the others nothing', async () => {const { machine, window, agentId, device, desk } = await fresh()onTab(window, [agentId])diff --git a/cli/src/architecture.spec.ts b/cli/src/architecture.spec.tsindex a9dfe600c..c299b8163 100644--- a/cli/src/architecture.spec.ts+++ b/cli/src/architecture.spec.ts@@ -293,7 +293,13 @@ const SERVICE_MAY_IMPORT: Record<string, string> = {//// Quiet-machine QA moves handoff history folding, redaction and file writes to the edge host;// the core keeps narrow conversation reads: 73,025 lines in 336 files after the command-bar extraction.-const CORE_CLOSURE_BUDGET = 73_125+//+// Grew by 229 to 73,254 in 337 (step 9, the devices on demand), from 73,025 in 336: the devices' process runs+// only once there is a device, about 72 MiB at idle that a computer with none no longer pays. What asks for+// it is the core's (core/devicesWake.ts, 146: a dial's port in /dev every two seconds, a paired Wi-Fi+// device); a Wi-Fi device's requests are held while it starts (core/wifi.ts, +50), and a process on demand+// that did not come in time is answered at once (core/serviceLinks.ts).+const CORE_CLOSURE_BUDGET = 73_355/** What is not the core's, by path: each goes to a service or its own process, in the plan's order. */const EDGE: RegExp[] = [diff --git a/cli/src/core/AGENTS.md b/cli/src/core/AGENTS.mdindex cf4dd257d..db9599c8f 100644--- a/cli/src/core/AGENTS.md+++ b/cli/src/core/AGENTS.md@@ -60,4 +60,5 @@ questions. Everything else is a service ([../services/AGENTS.md](../services/AGEanswers them. `wifi.ts`: the Wi-Fi device, as the core keeps it wherever its service runs (who saidhello, which transcripts and streams it follows, the focus revision, and the check on every answer itsends a device); `wifiAgents.ts`: its doors into the core (the agents as it lists them, a prompt, an- agent made for a Store harness); `wifiLink.ts`: its service in the devices' process.+ agent made for a Store harness); `wifiLink.ts`: its service in the devices' process. `devicesWake.ts`: when+ that process is asked for, once there is a device (a dial's port in /dev, a paired Wi-Fi device).diff --git a/cli/src/core/devicesWake.spec.ts b/cli/src/core/devicesWake.spec.tsnew file mode 100644index 000000000..a99b7a38e--- /dev/null+++ b/cli/src/core/devicesWake.spec.ts@@ -0,0 +1,162 @@+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { createDevicesWake, dialPortThere, DIAL_LOOK_MS, wifiDeviceSaved } from './devicesWake.js'++describe('a dial\'s port, as the core looks for one', () => {+ const look = (over: Partial<Parameters<typeof dialPortThere>[0]> = {}) =>+ dialPortThere({ platform: 'darwin', listDev: () => [], read: () => '[]', exists: () => false, ...over })++ it('is a USB modem\'s node in /dev: cu.usbmodem on macOS, ttyACM or ttyUSB on Linux, none elsewhere', () => {+ expect(look({ listDev: () => ['cu.debug-console', 'cu.Bluetooth-Incoming-Port', 'tty.usbmodem1101'] })).toBe(false)+ expect(look({ listDev: () => ['cu.debug-console', 'cu.usbmodem1101'] })).toBe(true)+ expect(look({ platform: 'linux', listDev: () => ['tty0', 'ttyS0'] })).toBe(false)+ expect(look({ platform: 'linux', listDev: () => ['ttyACM0'] })).toBe(true)+ expect(look({ platform: 'linux', listDev: () => ['ttyUSB1'] })).toBe(true)+ expect(look({ platform: 'win32', listDev: () => ['cu.usbmodem1101'] })).toBe(false)+ // /dev that cannot be read has no dial in it.+ expect(look({ listDev: () => { throw new Error('EACCES') } })).toBe(false)+ })++ it('is the end-to-end suite\'s dial when it names one: a pseudo-terminal that is there, or a list with one', () => {+ expect(look({ testDialPort: '/dev/ttys009', exists: (path) => path === '/dev/ttys009' })).toBe(true)+ expect(look({ testDialPort: '/dev/ttys009' })).toBe(false)+ expect(look({ testDialPort: '/d/dials.json', read: () => '[]' })).toBe(false)+ expect(look({ testDialPort: '/d/dials.json', read: () => '[{"serial":"A"}]' })).toBe(false)+ expect(look({ testDialPort: '/d/dials.json', read: () => '{"path":"/dev/ttys1"}' })).toBe(false)+ expect(look({ testDialPort: '/d/dials.json', read: () => '[{"serial":"A","path":"/dev/ttys1"}]' })).toBe(true)+ expect(look({ testDialPort: '/d/dials.json', read: () => 'half a file' })).toBe(false)+ // Only the suite's: a USB modem beside it is not looked at.+ expect(look({ testDialPort: '/d/dials.json', read: () => '[]', listDev: () => ['cu.usbmodem1'] })).toBe(false)+ })+})++describe('a paired Wi-Fi device', () => {+ let dataDir: string+ beforeEach(() => { dataDir = mkdtempSync(join(tmpdir(), 'devices-wake-')) })+ afterEach(() => rmSync(dataDir, { recursive: true, force: true }))++ it('is one the gateway\'s pairings name as a device, or one its direct links connect out to, read no further than a bound', () => {+ const links = join(dataDir, 'autonomous-device-connections.json')+ const pairings = join(dataDir, 'e2e', 'paired.json')+ mkdirSync(join(dataDir, 'e2e'))+ expect(wifiDeviceSaved(dataDir)).toBe(false)+ writeFileSync(links, '[]')+ writeFileSync(pairings, JSON.stringify([{ identityPub: 'p', label: 'phone', pairedAt: 1, role: 'web' }, null]))+ expect(wifiDeviceSaved(dataDir)).toBe(false)+ writeFileSync(pairings, JSON.stringify([{ identityPub: 'd', label: 'Desk', pairedAt: 1, role: 'device' }]))+ expect(wifiDeviceSaved(dataDir)).toBe(true)+ writeFileSync(pairings, '{"role":"device"}')+ expect(wifiDeviceSaved(dataDir)).toBe(false)+ writeFileSync(links, JSON.stringify([{ discoveryId: 'd', fingerprint: 'f' }]))+ expect(wifiDeviceSaved(dataDir)).toBe(true)+ writeFileSync(links, 'not json')+ expect(wifiDeviceSaved(dataDir)).toBe(false)+ writeFileSync(links, 'x'.repeat(1_048_577))+ expect(wifiDeviceSaved(dataDir, () => '[1]')).toBe(false)+ })+})++describe('when the devices\' process is asked for', () => {+ let dataDir: string+ beforeEach(() => { dataDir = mkdtempSync(join(tmpdir(), 'devices-wake-')); vi.useFakeTimers() })+ afterEach(() => { vi.useRealTimers(); rmSync(dataDir, { recursive: true, force: true }) })+ const both = new Set(['devices', 'wifi'])++ function wake(over: Partial<Parameters<typeof createDevicesWake>[0]> = {}) {+ let dev: string[] = []+ const want = vi.fn()+ const log = vi.fn()+ const devices = createDevicesWake({ want, dataDir, platform: 'darwin', listDev: () => dev, log, ...over })+ return { devices, want, log, plug: (names: string[]) => { dev = names } }+ }++ it('looks at /dev every two seconds until a dial\'s port is there, asks once, and looks no more', () => {+ const { devices, want, log, plug } = wake()+ devices.start(both)+ expect(want).not.toHaveBeenCalled()+ vi.advanceTimersByTime(DIAL_LOOK_MS * 3)+ expect(want).not.toHaveBeenCalled()+ plug(['cu.usbmodem1101'])+ vi.advanceTimersByTime(DIAL_LOOK_MS)+ expect(want.mock.calls).toEqual([['devices']])+ expect(log).toHaveBeenCalledWith('[devices] a dial\'s port is there: asking for the devices\' process')+ vi.advanceTimersByTime(DIAL_LOOK_MS * 5)+ expect(want).toHaveBeenCalledTimes(1)+ expect(vi.getTimerCount()).toBe(0)+ })++ it('asks at once for a dial already plugged in, or a paired Wi-Fi device', () => {+ const { devices, want, plug } = wake()+ plug(['cu.usbmodem1'])+ writeFileSync(join(dataDir, 'autonomous-device-connections.json'), JSON.stringify([{ discoveryId: 'd', fingerprint: 'f' }]))+ devices.start(both)+ expect(want.mock.calls).toEqual([['wifi'], ['devices']])+ expect(vi.getTimerCount()).toBe(0)+ })++ it('holds what was asked before the core was bound, and asks only for what runs in its own process', () => {+ const { devices, want } = wake()+ devices.ask('wifi', 'a Wi-Fi device')+ devices.ask('wifi', 'a Wi-Fi device')+ expect(want).not.toHaveBeenCalled()+ devices.start(new Set(['devices']))+ // The Wi-Fi device runs in the core's process here: nothing to ask for, and the dials are still looked for.+ expect(want).not.toHaveBeenCalled()+ expect(vi.getTimerCount()).toBe(1)+ devices.ask('devices', 'a request')+ expect(want.mock.calls).toEqual([['devices']])+ expect(vi.getTimerCount()).toBe(0)+ expect(devices.ask('other', 'a service in the core\'s process')).toBe(false)+ expect(devices.ask('wifi', 'a Wi-Fi device')).toBe(false)+ expect(devices.ask('devices', 'asked again')).toBe(true)+ expect(want).toHaveBeenCalledTimes(1)+ const early = wake()+ expect(early.devices.ask('wifi', 'a Wi-Fi device')).toBe(true)+ early.devices.start(both)+ expect(early.want.mock.calls).toEqual([['wifi']])+ })++ it('looks for nothing with the devices in the core\'s process or the serial ports left alone', () => {+ const inCore = wake()+ inCore.devices.start(new Set(['search']))+ const disabled = wake({ cableDisabled: true })+ disabled.devices.start(both)+ expect(vi.getTimerCount()).toBe(0)+ expect(inCore.want).not.toHaveBeenCalled()+ expect(disabled.want).not.toHaveBeenCalled()+ })++ it('stops looking once the process connected, whoever asked for it, and when the core stops', () => {+ const { devices, want, plug } = wake()+ devices.start(both)+ devices.connected('wifi')+ expect(vi.getTimerCount()).toBe(1)+ devices.connected('devices')+ expect(vi.getTimerCount()).toBe(0)+ plug(['cu.usbmodem1'])+ devices.ask('devices', 'again')+ expect(want).not.toHaveBeenCalled()+ const other = wake()+ other.devices.start(both)+ other.devices.stop()+ other.devices.stop()+ expect(vi.getTimerCount()).toBe(0)+ })++ it('reads /dev, the files and the clock of this computer when given none', () => {+ vi.useRealTimers()+ const log = vi.spyOn(console, 'log').mockImplementation(() => {})+ const want = vi.fn()+ const devices = createDevicesWake({ want, dataDir, testDialPort: join(dataDir, 'none.json') })+ devices.ask('devices', 'a test')+ devices.start(both)+ expect(want).toHaveBeenCalledWith('devices')+ expect(log).toHaveBeenCalledWith('[devices] a test: asking for the devices\' process')+ const looking = createDevicesWake({ want, dataDir })+ looking.start(both)+ looking.stop()+ log.mockRestore()+ })+})diff --git a/cli/src/core/devicesWake.ts b/cli/src/core/devicesWake.tsnew file mode 100644index 000000000..1d478b1e6--- /dev/null+++ b/cli/src/core/devicesWake.ts@@ -0,0 +1,146 @@+/**+ * When the devices' process starts (harnessd/services.ts `devices`, on demand since protocol 4): once there is+ * a device, so a computer with none never pays for it (about 72 MiB at idle, measured for step 9).+ *+ * What asks for it:+ * - **a dial's port**: a USB modem's callout node in /dev (`cu.usbmodem*` on macOS, `ttyACM*` or `ttyUSB*` on+ * Linux), looked for every two seconds, the dial's own scan's cadence (#909). A listing of /dev and nothing+ * more: no `ioreg`, no open, no read of a port. The devices' own scan then decides whether it is a dial.+ * - **a paired Wi-Fi device**, as the core starts: one the gateway's pairings name as a device, or one its direct+ * links connect out to, which start only once the device's service serves (gateway/start.ts `wifiService`).+ * Read once, bounded; the pairings' file holds public keys and labels.+ * - **a Wi-Fi device's session through the relay, its pairing, a request for the devices** (the Devices tab,+ * ⌘K): core/wifi.ts and the service links (`onDemand`) ask for it as they come.+ *+ * Once asked for, it is the master's to keep running, as every service is.+ */+import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'+import { join } from 'node:path'++/** The services in the devices' process, which a request or a notice for wakes. */+export const DEVICES_ON_DEMAND = ['devices', 'wifi'] as const+/** How often /dev is looked at for a dial's port while there is none: the dial's own scan's cadence. */+export const DIAL_LOOK_MS = 2_000+/** The Wi-Fi device's direct links (lib/autonomous-device/direct.ts) and the gateway's pairings (lib/e2ee/store.ts),+ * read no further than this. */+const DIRECT_LINKS = 'autonomous-device-connections.json'+const PAIRINGS = join('e2e', 'paired.json')+const SAVED_MAX = 1_048_576++/** Whether a dial's port may be there: a USB modem's node, or one of the end-to-end suite's dials (a+ * pseudo-terminal, or a `.json` list of them, as cable/cableFleet.ts `testDialDiscovery` reads it). */+export function dialPortThere(deps: { platform: NodeJS.Platform; listDev: () => string[]; testDialPort?: string; read: (file: string) => string; exists: (path: string) => boolean }): boolean {+ try {+ if (deps.testDialPort) {+ if (!deps.testDialPort.endsWith('.json')) return deps.exists(deps.testDialPort)+ const listed: unknown = JSON.parse(deps.read(deps.testDialPort))+ return Array.isArray(listed) && listed.some((row) => typeof row?.path === 'string')+ }+ const names = deps.platform === 'darwin' ? ['cu.usbmodem'] : deps.platform === 'linux' ? ['ttyACM', 'ttyUSB'] : []+ return names.length > 0 && deps.listDev().some((name) => names.some((prefix) => name.startsWith(prefix)))+ } catch {+ return false+ }+}++/** Whether a Wi-Fi device is paired here: the gateway's pairings name one as a device, or its direct links+ * connect out to one. */+export function wifiDeviceSaved(dataDir: string, read: (file: string) => string = (file) => readFileSync(file, 'utf8')): boolean {+ const rows = (name: string): unknown[] => {+ const file = join(dataDir, name)+ try {+ if (statSync(file).size > SAVED_MAX) return []+ const listed: unknown = JSON.parse(read(file))+ return Array.isArray(listed) ? listed : []+ } catch {+ return []+ }+ }+ return rows(DIRECT_LINKS).length > 0 || rows(PAIRINGS).some((pair) => (pair as { role?: unknown } | null)?.role === 'device')+}++export interface DevicesWakeDeps {+ /** Ask the master for the process that runs this service (harnessd/coreLink.ts `want`). */+ want(service: string): void+ dataDir: string+ /** `CABLE_DISABLE`: the serial ports are left alone, so no port asks for them. */+ cableDisabled?: boolean+ /** `HARNESSD_TEST_DIAL_PORT`: the end-to-end suite's dials. */+ testDialPort?: string+ platform?: NodeJS.Platform+ listDev?: () => string[]+ read?: (file: string) => string+ exists?: (path: string) => boolean+ setTimer?: (run: () => void, ms: number) => unknown+ clearTimer?: (timer: unknown) => void+ log?: (line: string) => void+}++export function createDevicesWake(deps: DevicesWakeDeps) {+ const read = deps.read ?? ((file: string) => readFileSync(file, 'utf8'))+ const setTimer = deps.setTimer ?? ((run, ms) => setTimeout(run, ms).unref())+ const clearTimer = deps.clearTimer ?? ((timer) => clearTimeout(timer as ReturnType<typeof setTimeout>))+ const log = deps.log ?? ((line: string) => console.log(line))+ const look = (): boolean => dialPortThere({+ platform: deps.platform ?? process.platform,+ listDev: deps.listDev ?? (() => readdirSync('/dev')),+ testDialPort: deps.testDialPort,+ read,+ exists: deps.exists ?? existsSync,+ })+ /** The services asked for; which run in their own process, once the core is bound (null before). */+ const asked = new Set<string>()+ let out: ReadonlySet<string> | null = null+ let timer: unknown = null+ const stopLooking = (): void => {+ if (timer !== null) clearTimer(timer)+ timer = null+ }+ const want = (service: string, why: string): void => {+ log(`[devices] ${why}: asking for the devices' process`)+ deps.want(service)+ }+ /** Asked before the master could hear it (the core not yet bound): asked again at `start`. */+ const early: Array<[string, string]> = []+ /** True when the service's process is asked for, or will be once the core is bound; false when the service+ * runs in the core's process, with nothing to ask for. */+ const ask = (service: string, why: string): boolean => {+ if (out && !out.has(service)) return false+ if (asked.has(service)) return true+ asked.add(service)+ if (service === 'devices') stopLooking()+ if (out) want(service, why)+ else early.push([service, why])+ return true+ }+ const lookAgain = (): void => {+ timer = setTimer(() => {+ timer = null+ if (look()) ask('devices', 'a dial\'s port is there')+ else lookAgain()+ }, DIAL_LOOK_MS)+ }++ return {+ /** The core is bound and its master hears it: what was asked meanwhile is asked now, and, with the dials+ * in their own process, a device already here asks for it, or /dev is looked at until one is. */+ start(outOfProcess: ReadonlySet<string>): void {+ out = outOfProcess+ for (const [service, why] of early.splice(0)) if (outOfProcess.has(service)) want(service, why)+ if (wifiDeviceSaved(deps.dataDir, read)) ask('wifi', 'a paired Wi-Fi device')+ if (!outOfProcess.has('devices') || asked.has('devices') || deps.cableDisabled) return+ if (look()) ask('devices', 'a dial\'s port is there')+ else lookAgain()+ },+ /** A device, a pairing or a request needs this service (core/wifi.ts). */+ ask,+ /** The process connected: the master runs it, whoever asked (or it was named to start with the others). */+ connected(service: string): void {+ asked.add(service)+ if (service === 'devices') stopLooking()+ },+ stop: stopLooking,+ }+}++export type DevicesWake = ReturnType<typeof createDevicesWake>diff --git a/cli/src/core/main.ts b/cli/src/core/main.tsindex 29dbb2dc5..64fb3e8f6 100644--- a/cli/src/core/main.ts+++ b/cli/src/core/main.ts@@ -146,7 +146,8 @@ import { publishHookRoute } from '../lib/hookRoutes.js'import { routedCommandBar } from '../lib/commandBarHttp.js'import { BackendSocket, isLocalClientId } from '../backendSocket.js'import { createGatewayLink, laneOf } from './gatewayLink.js'-import { createWifiCore } from './wifi.js'+import { createWifiCore, WIFI_START_MS } from './wifi.js'+import { createDevicesWake, DEVICES_ON_DEMAND } from './devicesWake.js'import { createWifiLink } from './wifiLink.js'import { wifiDoors } from './wifiAgents.js'import { autonomousDeviceLocalRequest } from '../lib/autonomous-device/localApi.js'@@ -278,6 +279,9 @@ function startMasterHere(): void {/** This process's channel to a harnessd master, when one started it (see harnessd/coreLink.ts).* Inert otherwise: a daemon run on its own claims its pid file and hands off updates itself. */const coreLink = connectToMaster()+/** When the devices' process starts: once there is a device, a dial's port, a Wi-Fi device or a request for one+ * (core/devicesWake.ts). Asked of the master through the channel above. */+const devicesWake = createDevicesWake({ want: (service) => coreLink.want(service), dataDir: env.ADAPTER_DATA_DIR, cableDisabled: env.CABLE_DISABLE, testDialPort: process.env.HARNESSD_TEST_DIAL_PORT })const daemonBoot: {updater: Poller | null@@ -645,6 +649,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {// Its direct links are the gateway's, started only once it serves: a device they connected to a service// that could not be built would be answered by nothing.ready: () => gatewayOps.wifiService(true),+ want: () => devicesWake.ask('wifi', 'a Wi-Fi device'),doors: wifiDoors({registry, machineId: machineIdNow, displayName: projectDisplayName, running: (sessionId) => turnStartedAt.has(sessionId),socket: () => backendRef ?? null, deviceInput: () => deviceInput, answer: (request) => questions.answer(request),@@ -1185,8 +1190,8 @@ async function runForeground(session: AuthSession | null): Promise<void> {token: serviceToken,owned: Object.fromEntries([...outOfProcess].map((name) => [name, requestsOf[name] ?? []])),waits: LONG_ANSWERS,- // An experiment's process runs only once it is on: a request for one that is off asks the master for it.- onDemand: experiments,+ // An experiment's process runs once it is on, the devices' once there is one: a request for one off asks for it.+ onDemand: new Set([...experiments, ...DEVICES_ON_DEMAND]),want: (service) => experimentHooks.want(service),answer: async (service, query, payload) => deliveries.answer(service, query, payload)?? await answerExperimentQuery(coreApi, experiments, service, query, payload)@@ -1210,6 +1215,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {if (service === 'teams') teamsLink.on()if (service === 'devices') devicesLink.connected()if (service === 'wifi') wifiCore.started(appVoiceFocus ?? null)+ devicesWake.connected(service)},disconnected: (service) => {if (service === 'gateway') gatewayLink?.disconnected()@@ -1722,8 +1728,8 @@ async function runForeground(session: AuthSession | null): Promise<void> {return answer.result}return autonomousDeviceLocalRequest({- discover: () => viaGateway({ op: 'discover' }),- pairStart: ({ code, device }) => viaGateway({ op: 'pair', code, device }),+ discover: async () => { await wifiCore.serving(WIFI_START_MS); return viaGateway({ op: 'discover' }) },+ pairStart: async ({ code, device }) => { await wifiCore.serving(WIFI_START_MS); return viaGateway({ op: 'pair', code, device }) },pairStatus: () => viaGateway({ op: 'pairStatus' }),list: () => viaGateway({ op: 'list' }),status: async () => ({ transport: 'direct', connected: wifiCore.directSessions() > 0,@@ -1801,6 +1807,7 @@ async function runForeground(session: AuthSession | null): Promise<void> {// The experiments with saved state are on: their processes are asked for now, the master heeding a// bound core alone (core/experiments.ts). The others wait for their first request.wakeExperiments({ dataDir: env.ADAPTER_DATA_DIR, experiments: EXPERIMENTS, outOfProcess, want: (service) => experimentHooks.want(service) })+ devicesWake.start(outOfProcess)} else {try { writeFileSync(PID_FILE, String(process.pid) + '\n') } catch { /* best effort */ }}diff --git a/cli/src/core/serviceLinks.spec.ts b/cli/src/core/serviceLinks.spec.tsindex eb15a42b3..aaaee7d60 100644--- a/cli/src/core/serviceLinks.spec.ts+++ b/cli/src/core/serviceLinks.spec.ts@@ -138,16 +138,27 @@ describe('service links', () => {expect(reply).toHaveBeenCalledWith({ link: 'ready' })})- it('answers SERVICE_UNAVAILABLE for an experiment that does not come in time, or will not take the request', () => {- const links = make({ owned: { orchestrator: ['orchestrator'], search: ['session_search'] }, onDemand: new Set(['orchestrator']) })+ it('answers SERVICE_UNAVAILABLE for one on demand that does not come in time, then at once until it comes, or will not take the request', () => {+ const want = vi.fn()+ const links = make({ owned: { orchestrator: ['orchestrator'], devices: ['harness_devices_list'], search: ['session_search'] }, onDemand: new Set(['orchestrator', 'devices']), want })const late = vi.fn()links.route('orchestrator', { action: 'list' }, ASKER, late)vi.advanceTimersByTime(5_000)expect(late).toHaveBeenCalledWith({ error: 'SERVICE_UNAVAILABLE', service: 'orchestrator', retryable: true })+ // Its master could not start it: the next request does not wait the whole wait again, nor ask again.+ const again = vi.fn()+ links.route('orchestrator', { action: 'list' }, ASKER, again)+ expect(again).toHaveBeenCalledWith({ error: 'SERVICE_UNAVAILABLE', service: 'orchestrator', retryable: true })+ expect(want).toHaveBeenCalledTimes(1)+ // Once it comes, it is served as any other.+ const served = sink()+ links.accept('orchestrator', TOKEN, served, vi.fn())+ links.route('orchestrator', { action: 'list' }, ASKER, vi.fn())+ expect(served.sent).toHaveLength(1)const refused = vi.fn()- links.route('orchestrator', { action: 'list' }, ASKER, refused)- links.accept('orchestrator', TOKEN, sink(false), vi.fn())- expect(refused).toHaveBeenCalledWith({ error: 'SERVICE_UNAVAILABLE', service: 'orchestrator', retryable: true })+ links.route('harness_devices_list', {}, ASKER, refused)+ links.accept('devices', TOKEN, sink(false), vi.fn())+ expect(refused).toHaveBeenCalledWith({ error: 'SERVICE_UNAVAILABLE', service: 'devices', retryable: true })// A service that is not an experiment is never waited for.const search = vi.fn()links.route('session_search', {}, ASKER, search)diff --git a/cli/src/core/serviceLinks.ts b/cli/src/core/serviceLinks.tsindex 133938ee9..34ad9e498 100644--- a/cli/src/core/serviceLinks.ts+++ b/cli/src/core/serviceLinks.ts@@ -57,11 +57,12 @@ export interface ServiceLinksOptions {/** Longer waits for the answers that take longer, by service and then by type (core/api.ts* `LONG_ANSWERS`): a grid command, grid's set-up, a harness's install. */waits?: Readonly<Record<string, Readonly<Record<string, number>>>>- /** The experiments: services whose process runs only once it is on (core/api.ts `EXPERIMENTS`). A request- * for one that has not connected yet asks for it (`want`) and waits for it to connect, within the same- * time. One that has connected and is down again is answered as any service is, at once. */+ /** The services whose process runs only once asked for: the experiments (core/api.ts `EXPERIMENTS`) and the+ * devices (core/devicesWake.ts). A request for one that has not connected yet asks for it (`want`) and waits+ * for it to connect, within the same time. One that has connected and is down again is answered as any+ * service is, at once; so is one that did not connect within a request's wait, until it does. */onDemand?: ReadonlySet<string>- /** Ask the master for an experiment's process (harnessd/coreLink.ts `want`). */+ /** Ask the master for a process on demand (harnessd/coreLink.ts `want`). */want?(service: string): voidlog?: (line: string) => voidnewId?: () => string@@ -100,6 +101,10 @@ export function createServiceLinks(options: ServiceLinksOptions) {const links = new Map<string, Connected>()/** The services that have connected in this core's life: an experiment among them is on, not off. */const seen = new Set<string>()+ /** Asked for and not connected within a request's wait: the master could not start it (it ends as it starts,+ * and is parked). Its requests are answered at once, as a service that is down is, instead of each waiting+ * the whole wait again (⌘K's 25 s, found end to end with the devices crashing on every start). */+ const unstarted = new Set<string>()const waiting = new Map<string, Waiting>()/** What a service must hear even if it is down when it is said (a purge's forgetting), delivered on* its next connection; bounded, the oldest dropped first. */@@ -129,13 +134,16 @@ export function createServiceLinks(options: ServiceLinksOptions) {* not connected is asked for, and the request goes once it connects: off, it costs nothing until asked. */const ask = (service: string, type: string, payload: Record<string, unknown>, asker: Asker, reply: (result: Record<string, unknown>) => void, waitMs?: number): void => {const link = links.get(service)- const starting = !link && !!options.onDemand?.has(service) && !seen.has(service)+ const starting = !link && !!options.onDemand?.has(service) && !seen.has(service) && !unstarted.has(service)if (!link && !starting) { reply(unavailable(service)); return }const id = newId()const frame: ServiceFrame = { type, payload: { ...payload, requestId: id }, asker }const entry: Waiting = { service, type, reply, timer: null, ...(starting ? { frame } : {}) }// Cleared whenever the entry is settled, so it only ever fires for one still waiting.- entry.timer = setTimer(() => settle(id, entry, unavailable(service)), waitMs ?? waitFor(service, type))+ entry.timer = setTimer(() => {+ if (entry.frame) unstarted.add(service)+ settle(id, entry, unavailable(service))+ }, waitMs ?? waitFor(service, type))waiting.set(id, entry)if (starting) { options.want?.(service); return }if (!link!.sink.sendFrame(frame)) settle(id, entry, unavailable(service))diff --git a/cli/src/core/wifi.spec.ts b/cli/src/core/wifi.spec.tsindex 58cc332bf..5598097f1 100644--- a/cli/src/core/wifi.spec.ts+++ b/cli/src/core/wifi.spec.ts@@ -1,7 +1,7 @@-import { describe, expect, it, vi } from 'vitest'+import { afterEach, describe, expect, it, vi } from 'vitest'import type { LiveEvent } from '../lib/normalize.js'import { WIFI_OFF, type RemoteClient, type WifiPort } from './api.js'-import { createWifiCore } from './wifi.js'+import { createWifiCore, WIFI_HELD_MAX, WIFI_HELD_MS } from './wifi.js'const device = (identity: string, direct = false): RemoteClient => ({ role: 'device', label: 'Desk', identity, direct })@@ -17,9 +17,12 @@ function harness() {const joined = vi.fn()const ready = vi.fn()const fullText = vi.fn((agentId: string) => (agentId === 'a' ? 'the whole answer' : undefined))- const wifi = createWifiCore({ port: () => on, gateway, remoteClient: (connId) => clients.get(connId) ?? null, fullText, joined, ready, doors: WIFI_OFF })- return { wifi, port, clients, gateway, joined, ready, fullText, off: () => { on = null } }+ const want = vi.fn(() => true)+ const wifi = createWifiCore({ port: () => on, gateway, remoteClient: (connId) => clients.get(connId) ?? null, fullText, joined, ready, want, doors: WIFI_OFF })+ return { wifi, port, clients, gateway, joined, ready, fullText, want, off: () => { on = null }, on: () => { on = port } }}+/** What `started` hands on after the service was resumed, a few promise turns later. */+const settled = async (): Promise<void> => { for (let i = 0; i < 4; i++) await Promise.resolve() }describe('the Wi-Fi device, as the core keeps it', () => {it('counts a device as watching only while its app said hello on a session still its own, with the service there', () => {@@ -159,8 +162,10 @@ describe('the Wi-Fi device, as the core keeps it', () => {expect(fullText).toHaveBeenCalledTimes(1)})- it('passes the gateway\'s device sessions on, and forgets a phone\'s', () => {+ it('passes the gateway\'s device sessions on, and forgets a phone\'s', async () => {const { wifi, port, gateway } = harness()+ wifi.started(null)+ await settled()wifi.fromGateway.session('phone', { role: 'web', label: null, identity: 'p', direct: false })wifi.fromGateway.session('phone', null)expect(port.session).not.toHaveBeenCalled()@@ -225,3 +230,108 @@ describe('the Wi-Fi device, as the core keeps it', () => {expect(wifi.focusRevision()).toBeUndefined()})})++describe('the Wi-Fi device while its service is not there', () => {+ afterEach(() => { vi.useRealTimers() })+ const hello = { type: 'autonomous_device_request' }++ it('asks for its process when a device\'s session comes, and not for a phone\'s or once it is there', async () => {+ const { wifi, want } = harness()+ wifi.fromGateway.session('phone', { role: 'web', label: null, identity: 'p', direct: false })+ wifi.fromGateway.session('c1', null)+ expect(want).not.toHaveBeenCalled()+ wifi.fromGateway.session('c1', device('id-1'))+ expect(want).toHaveBeenCalledTimes(1)+ wifi.started(null)+ wifi.fromGateway.session('c2', device('id-2'))+ expect(want).toHaveBeenCalledTimes(1)+ })++ it('holds what its devices send, asks for it, and hands it on in order once the service has been resumed', async () => {+ const { wifi, port, want } = harness()+ wifi.fromGateway.session('c1', device('id-1'))+ wifi.fromGateway.request('c1', hello, { payload: { n: 1 } })+ wifi.fromGateway.request('c1', hello, { payload: { n: 2 } })+ expect(port.request).not.toHaveBeenCalled()+ expect(want).toHaveBeenCalledTimes(3)+ wifi.started(null)+ // Not before the resume has been said: a request for a session the service has not been handed is lost.+ wifi.fromGateway.request('c1', hello, { payload: { n: 3 } })+ expect(port.request).not.toHaveBeenCalled()+ await settled()+ expect(port.resume).toHaveBeenCalledTimes(1)+ expect(port.request.mock.calls.map((call) => ((call as unknown[])[2] as { payload: { n: number } }).payload.n)).toEqual([1, 2, 3])+ expect(port.resume.mock.invocationCallOrder[0]).toBeLessThan(port.request.mock.invocationCallOrder[0])+ // From then on, straight through; and held again while the service is away (its process starting again).+ wifi.fromGateway.request('c1', hello, { payload: { n: 4 } })+ expect(port.request).toHaveBeenCalledTimes(4)+ wifi.stopped()+ wifi.fromGateway.request('c1', hello, { payload: { n: 5 } })+ expect(port.request).toHaveBeenCalledTimes(4)+ wifi.started(null)+ await settled()+ expect(port.request).toHaveBeenCalledTimes(5)+ })++ it('hands on a request held only as long as its device would wait for it, and holds a bounded number', async () => {+ vi.useFakeTimers()+ const { wifi, port } = harness()+ const sent = (): number[] => port.request.mock.calls.map((call) => ((call as unknown[])[2] as { payload: { n: number } }).payload.n)+ wifi.fromGateway.request('c1', hello, { payload: { n: 0 } })+ vi.advanceTimersByTime(WIFI_HELD_MS + 1)+ wifi.fromGateway.request('c1', hello, { payload: { n: 1 } })+ wifi.started(null)+ await settled()+ // The stale one is dropped: the device retried it by its key, or gave up on it.+ expect(sent()).toEqual([1])+ const bounded = harness()+ for (let n = 0; n <= WIFI_HELD_MAX; n++) bounded.wifi.fromGateway.request('c1', hello, { payload: { n } })+ bounded.wifi.started(null)+ await settled()+ const kept = bounded.port.request.mock.calls.map((call) => ((call as unknown[])[2] as { payload: { n: number } }).payload.n)+ // The oldest beyond the bound goes first.+ expect(kept).toHaveLength(WIFI_HELD_MAX)+ expect(kept[0]).toBe(1)+ })++ it('keeps holding while the service went before it was handed what it held or has no port, and hands it on after a failed resume', async () => {+ const { wifi, port, off, on } = harness()+ wifi.fromGateway.request('c1', hello, { payload: { n: 1 } })+ wifi.started(null)+ wifi.stopped()+ await settled()+ expect(port.request).not.toHaveBeenCalled()+ off()+ wifi.started(null)+ await settled()+ on()+ port.resume.mockRejectedValueOnce(new Error('gone'))+ wifi.started(null)+ await settled()+ expect(port.request).toHaveBeenCalledTimes(1)+ })++ it('waits for the service to serve, asking for it, before a pairing goes through the direct links', async () => {+ vi.useFakeTimers()+ const { wifi, want, ready } = harness()+ const served = wifi.serving(1_000)+ expect(want).toHaveBeenCalledTimes(1)+ wifi.started(null)+ wifi.api.ready()+ expect(ready).toHaveBeenCalled()+ await expect(served).resolves.toBe(true)+ // Serving now: at once, without asking.+ await expect(wifi.serving(1_000)).resolves.toBe(true)+ expect(want).toHaveBeenCalledTimes(1)+ // Gone again: asked for, and not there in time.+ wifi.stopped()+ const late = wifi.serving(1_000)+ vi.advanceTimersByTime(1_001)+ await expect(late).resolves.toBe(false)+ wifi.api.ready()+ // In the core's process, with nothing to ask for: not waited on.+ wifi.stopped()+ want.mockReturnValueOnce(false)+ await expect(wifi.serving(1_000)).resolves.toBe(false)+ })+})diff --git a/cli/src/core/wifi.ts b/cli/src/core/wifi.tsindex a2d0b6a41..c26bc6363 100644--- a/cli/src/core/wifi.ts+++ b/cli/src/core/wifi.ts@@ -18,6 +18,9 @@* none while it is off, so a selection is refused then, as it was with no service.* - **Each answer goes to the session it came from:** the core seals it to a session only while that* session is still the same identity's device.+ * - **What a device sends while its service is not there** (its process starting at the first device, or+ * starting again) is held, in order, and handed on once the service has been resumed; a session, a+ * request or a pairing asks for the process (core/devicesWake.ts).*/import type { LiveEvent } from '../lib/normalize.js'import type { DeviceInputStatus } from './deviceInput.js'@@ -60,6 +63,9 @@ export interface WifiCoreDeps {joined(): void/** The service is built and serving: the gateway's direct links may connect devices to it. */ready(): void+ /** Ask for the service's process: a device needs it (core/devicesWake.ts). False when there is none to ask+ * for: the service runs in the core's process, and is there or failed as the core started. */+ want(): boolean/** The doors that are one call each into the core's own modules. */doors: Pick<CoreApi['wifi'], 'view' | 'submit' | 'cancel' | 'started' | 'stop' | 'answer' | 'create' | 'stepFocus' | 'scroll' | 'focusApp' | 'reveal'>}@@ -70,6 +76,13 @@ const PROVEN_BY_TRANSCRIPT = new Set(['claude', 'codex'])* always, and a tool's start and end, for an agent a device subscribed to. */const ALWAYS_STREAMED = new Set(['turn_started', 'text_delta', 'turn_ended'])const TOOL_EVENTS = new Set(['tool_start', 'tool_end'])+/** A device's requests held while its service is not there: the most, and how old one may be when handed on.+ * The device retries a request it had no answer to by the same key, so one older than this has been sent+ * again, or given up on; the process starts in about a second. */+export const WIFI_HELD_MAX = 256+export const WIFI_HELD_MS = 30_000+/** How long a pairing waits for the service to serve: its process starts in about a second, more under load. */+export const WIFI_START_MS = 15_000export function createWifiCore(deps: WifiCoreDeps) {const { port, gateway, remoteClient } = deps@@ -84,6 +97,19 @@ export function createWifiCore(deps: WifiCoreDeps) {let streamed = new Set<string>()let revision: string | undefinedlet up = false+ /** Whether the service said it serves, since it was last there; and who waits for that (a pairing). */+ let serves = false+ const servingWaiters = new Set<() => void>()+ /** A device's requests while the service is not there, or before what it was handed has gone first. */+ const held: Array<{ connId: string; frame: Record<string, unknown>; opened: Record<string, unknown> | null; at: number }> = []+ let holding = true+ const release = (): void => {+ const service = port()+ if (!up || !service) return+ const now = Date.now()+ for (const each of held.splice(0)) if (now - each.at <= WIFI_HELD_MS) void service.request(each.connId, each.frame, each.opened)+ holding = false+ }/** A session that is still the identity's device it said hello as. */const serving = (connId: string, identity: string): boolean => {@@ -113,7 +139,12 @@ export function createWifiCore(deps: WifiCoreDeps) {gateway.deviceClient(connId, identity)},joined: () => deps.joined(),- ready: () => deps.ready(),+ // The gateway told first, so a pairing waiting on this finds the direct links started.+ ready: () => {+ serves = true+ deps.ready()+ for (const served of [...servingWaiters]) served()+ },unpaired: (identity) => gateway.revokeIdentity(identity),focus: (next) => { revision = next },transcripts: (agentId, seen) => { if ((dispatched.get(agentId) ?? 0) <= seen) watched.delete(agentId) },@@ -155,9 +186,16 @@ export function createWifiCore(deps: WifiCoreDeps) {session: (connId: string, client: RemoteClient | null): void => {if (client?.role === 'device') sessions.set(connId, client)else if (!sessions.delete(connId)) return+ if (!up && client?.role === 'device') deps.want()port()?.session(connId, client?.role === 'device' ? client : null)},request: (connId: string, frame: Record<string, unknown>, opened: Record<string, unknown> | null): void => {+ if (holding) {+ held.push({ connId, frame, opened, at: Date.now() })+ if (held.length > WIFI_HELD_MAX) held.shift()+ if (!up) deps.want()+ return+ }port()?.request(connId, frame, opened)},dropped: (connId: string): void => {@@ -186,7 +224,8 @@ export function createWifiCore(deps: WifiCoreDeps) {directSessions: (): number => servingCount((client) => client.direct),/** The focus revision, as the service last said; none while it is off. */focusRevision: (): string | undefined => (up ? revision : undefined),- /** The service is there (it started here, or its process connected): it is handed what the core holds. */+ /** The service is there (it started here, or its process connected): it is handed what the core holds,+ * and then what its devices sent meanwhile, in order, before anything they send from now on. */started: (focus: WifiResume['focus']): void => {up = truewatched.clear()@@ -194,17 +233,32 @@ export function createWifiCore(deps: WifiCoreDeps) {streamed = new Set()revision = undefinedfor (const [connId, identity] of [...helloed]) if (!serving(connId, identity)) forget(connId)- port()?.resume({+ const resumed = port()?.resume({sessions: [...sessions].map(([connId, client]) => ({ connId, client })),helloed: [...helloed].map(([connId, identity]) => ({ connId, identity })),focus,})+ void Promise.resolve(resumed).catch(() => {}).then(release)},- /** The service went away: nothing it watched is sent, and no device is served until it is back. */+ /** The service went away: nothing it watched is sent, and no device is served until it is back; what its+ * devices send meanwhile is held for it. */stopped: (): void => {up = false+ serves = false+ holding = truerevision = undefined},+ /** Ask for the service and wait, at most `waitMs`, until it serves: a device's discovery and pairing go through+ * the gateway's direct links, which start only then. False when it did not in time. */+ serving: (waitMs: number): Promise<boolean> => {+ if (up && serves) return Promise.resolve(true)+ if (!deps.want()) return Promise.resolve(false)+ return new Promise((resolve) => {+ const served = (): void => { servingWaiters.delete(served); clearTimeout(timer); resolve(true) }+ const timer = setTimeout(() => { servingWaiters.delete(served); resolve(false) }, waitMs)+ servingWaiters.add(served)+ })+ },}}diff --git a/cli/src/harnessd/coreLink.ts b/cli/src/harnessd/coreLink.tsindex 9409ba937..897563f90 100644--- a/cli/src/harnessd/coreLink.ts+++ b/cli/src/harnessd/coreLink.ts@@ -67,7 +67,8 @@ export interface CoreLink {/** Start-up is done and requests are served — or it gave way to safe mode, and why: the master stops* waiting for it either way, and rolls back an update whose first core ends up in safe mode. */ready(safeMode?: string): void- /** Ask the master to start the experiment's process that runs [service]: it is on (protocol 3). */+ /** Ask the master to start the process on demand that runs [service]: an experiment that is on (protocol 3),+ * or the devices, now that there is one (protocol 4). */want(service: string): void/** Tell the master, every `heartbeatInterval`, that this core is alive and how big it is. */startHeartbeat(): voiddiff --git a/cli/src/harnessd/master.spec.ts b/cli/src/harnessd/master.spec.tsindex c9c5f9e34..c26d61c89 100644--- a/cli/src/harnessd/master.spec.ts+++ b/cli/src/harnessd/master.spec.ts@@ -304,8 +304,10 @@ describe('runMaster', { timeout: 60_000 }, () => {writeFileSync(core, `// The services run this script too (as \`__service <name>\`); each records its own.require('node:fs').writeFileSync(${JSON.stringify(ran)} + (process.argv[3] ?? ''), process.execPath)- process.send({ type: 'harnessd:bound', protocol: 2, port: 1 })+ process.send({ type: 'harnessd:bound', protocol: ${HARNESSD_PROTOCOL}, port: 1 })process.send({ type: 'harnessd:ready' })+ // A device: the core asks for the devices' process, which runs only on demand.+ if (process.argv[2] === '__run') process.send({ type: 'harnessd:want', service: 'wifi' })setInterval(() => process.send({ type: 'harnessd:heartbeat', rssBytes: 1, heapUsedBytes: 1, loopDelayMs: 0 }), 50)process.on('SIGTERM', () => process.exit(0))`)@@ -317,9 +319,11 @@ describe('runMaster', { timeout: 60_000 }, () => {exit: (code) => exits.push(code), onSignal: (signal, listener) => signals.set(signal, listener),})await until('the core and a service to be running', () => supervisor.status().state === 'running' && existsSync(ran) && existsSync(`${ran}search`))+ await until('the devices\' process the core asked for', () => existsSync(`${ran}devices,wifi`))const libexec = join(realpathSync(runtime), 'node-v1', 'libexec', 'harnessd')expect(readFileSync(ran, 'utf8')).toBe(join(libexec, 'harnessd-core'))expect(readFileSync(`${ran}search`, 'utf8')).toBe(join(libexec, 'harnessd-search'))+ expect(readFileSync(`${ran}devices,wifi`, 'utf8')).toBe(join(libexec, 'harnessd-devices'))signals.get('SIGTERM')!()await until('the master to finish', () => exits.length > 0)})diff --git a/cli/src/harnessd/master.ts b/cli/src/harnessd/master.tsindex 929fc1cb5..68c3c65d6 100644--- a/cli/src/harnessd/master.ts+++ b/cli/src/harnessd/master.ts@@ -345,8 +345,9 @@ export function runMaster(config: MasterConfig): Supervisor {confirmUpdate: config.confirmUpdate,bundle,log,- // An experiment's process, started once the core says it is on (./services.ts `onDemand`).+ // A process on demand, started once the core asks (./services.ts `onDemand`), or as a core too old to ask binds.want: (service) => services.want(service),+ unasked: (protocol) => services.unasked(protocol),// The services go with the master, after the core: none is left holding the core's socket.exit: (code) => {stopping = truediff --git a/cli/src/harnessd/protocol.ts b/cli/src/harnessd/protocol.tsindex d9c11d9e8..3a2af1c22 100644--- a/cli/src/harnessd/protocol.ts+++ b/cli/src/harnessd/protocol.ts@@ -12,8 +12,9 @@*/import type { SupervisorStatus } from './supervisor.js'-/** 2: `ready`, the event-loop delay on the heartbeat, and the exit-code contract below. 3: `want`. */-export const HARNESSD_PROTOCOL = 3+/** 2: `ready`, the event-loop delay on the heartbeat, and the exit-code contract below. 3: `want`. 4: `want`+ * for the devices' process too, which runs only once there is a device (./services.ts `askedSince`). */+export const HARNESSD_PROTOCOL = 4/** The command a bundle's master answers its probe on, and what it answers. */export const PROBE_COMMAND = '__harnessd-probe'@@ -51,8 +52,9 @@ export type CoreMessage =/** Sent every few seconds; a core that stops sending is hung. `loopDelayMs`: the longest the event* loop was held since the last one (protocol 2). */| { type: 'harnessd:heartbeat'; rssBytes: number; heapUsedBytes: number; loopDelayMs?: number }- /** Start the experiment's process that runs this service (harnessd/services.ts `onDemand`): it is on now- * (protocol 3). A master from before ignores it, having started every service at once. */+ /** Start the process that runs this service (harnessd/services.ts `onDemand`): an experiment that is on now+ * (protocol 3), or the devices, now that there is one (protocol 4). A master from before ignores it, having+ * started every service at once. */| { type: 'harnessd:want'; service: string }export type MasterMessage =diff --git a/cli/src/harnessd/services.spec.ts b/cli/src/harnessd/services.spec.tsindex 26648029e..11657696e 100644--- a/cli/src/harnessd/services.spec.ts+++ b/cli/src/harnessd/services.spec.ts@@ -272,13 +272,23 @@ describe('ServiceSupervisor', () => {expect(lines.filter((line) => line.includes('service experiments started'))).toHaveLength(2)})- it('starts every experiment for a core that never asks, and stops one never started at once', () => {+ it('starts every process on demand a core too old to ask for it would never ask for, and stops one never started at once', () => {const experiment: ServiceSpec = { name: 'orchestrator', services: ['orchestrator'], heapLimitMiB: 256, rssLimitMiB: 512, onDemand: true }+ const devices: ServiceSpec = { name: 'devices', services: ['devices', 'wifi'], heapLimitMiB: 256, rssLimitMiB: 512, onDemand: true, askedSince: 4 }const idle: ServiceSpec = { ...experiment, name: 'teams', services: ['teams'] }- const supervisor = make([experiment])+ // A core of protocol 3 asks for the experiments, not for the devices: theirs starts as it binds.+ const three = make([search, experiment, devices])+ three.start()+ three.unasked(3)+ expect(children.map((child) => child.spec.name)).toEqual(['search', 'devices'])+ three.unasked(4)+ expect(children).toHaveLength(2)+ // A core from before `want` asks for none.+ children.length = 0+ const supervisor = make([experiment, devices])supervisor.start()- supervisor.want(null)- expect(children.map((child) => child.spec.name)).toEqual(['orchestrator'])+ supervisor.unasked(2)+ expect(children.map((child) => child.spec.name)).toEqual(['orchestrator', 'devices'])const never = make([idle])never.start()const done = vi.fn()@@ -360,6 +370,14 @@ describe('ServiceSupervisor', () => {expect(serviceSpecs({ HARNESSD_SERVICES: 'search' }, hosts)).toEqual([search])})+ it('runs the devices only once there is one, asked for by a core of protocol 4, and from the start when named (tests)', () => {+ const [devices] = serviceSpecs({}, SERVICE_HOSTS).filter((spec) => spec.name === 'devices')+ expect(devices).toMatchObject({ services: ['devices', 'wifi'], onDemand: true, askedSince: 4 })+ const [named] = serviceSpecs({ HARNESSD_SERVICES: 'devices' }, SERVICE_HOSTS)+ expect(named.services).toEqual(['devices', 'wifi'])+ expect(named.onDemand).toBeUndefined()+ })+it('knows every service each process this build runs hosts, and each in one process only', () => {const hosted = Object.values(SERVICE_HOSTS).flatMap((host) => host.services)expect(KNOWN_SERVICES).toEqual(hosted)diff --git a/cli/src/harnessd/services.ts b/cli/src/harnessd/services.tsindex 1586bbcde..103ad9f9a 100644--- a/cli/src/harnessd/services.ts+++ b/cli/src/harnessd/services.ts@@ -24,9 +24,12 @@ export interface ServiceSpec {heapLimitMiB: number/** Resident memory past which it is restarted, MiB; 0: off. */rssLimitMiB: number- /** An experiment's process: not started with the others, only once the core asks for one of its- * services (`want`), and then kept running like any other. */+ /** A process on demand (an experiment's, the devices'): not started with the others, only once the core+ * asks for one of its services (`want`), and then kept running like any other. */onDemand?: boolean+ /** The core protocol (./protocol.ts) from which a core asks for this process on demand; 3, the experiments',+ * when unset. A core that speaks an older one never asks for it, and it is started as that core binds. */+ askedSince?: number}export interface ServiceSupervisorDeps {@@ -151,8 +154,8 @@ class Service {this.watchHeartbeat(child)}- /** Start an experiment's process the core asked for: once, while it is off. One already started is- * the master's to keep running, parked included. */+ /** Start a process on demand the core asked for: once, while it is off. One already started is the+ * master's to keep running, parked included. */want(): void {if (this.state === 'off') this.start()}@@ -283,18 +286,23 @@ export class ServiceSupervisor {this.services = specs.map((spec) => new Service(spec, deps, options, env))}- /** Start every service but the experiments; none waits on another, or on the core. */+ /** Start every service but those on demand; none waits on another, or on the core. */start(): void {for (const service of this.services) if (!service.spec.onDemand) service.start()}+ /** Start the process on demand that hosts [service], if it is not running yet: the core asked for it (`harnessd:want`). */+ want(service: string): void {+ for (const each of this.services) if (each.spec.services.includes(service)) each.want()+ }+/**- * Start the experiment's process that hosts [service], if it is not running yet: the core asked for it- * (`harnessd:want`). With null, every experiment's: a core from before `want` never asks, and ran them- * all as it ran every other service.+ * A core bound that speaks [protocol], older than this master's: every process that became on demand after it+ * starts now, since that core never asks for one and ran it as it ran every other service. A core from before+ * `want` (2) asks for none; one from before the devices were on demand (3) never asks for theirs.*/- want(service: string | null): void {- for (const each of this.services) if (service === null || each.spec.services.includes(service)) each.want()+ unasked(protocol: number): void {+ for (const each of this.services) if (each.spec.onDemand && (each.spec.askedSince ?? 3) > protocol) each.want()}/** Stop every service; `done` once all are gone. */@@ -360,8 +368,10 @@ export const SERVICE_HOSTS: Readonly<Record<string, ServiceHostSpec>> = {// is bounded per dial), the window bridges, the fleet's router and its lane, the voice router's engine// worker (a process of its own, outside this budget). Hardware that speaks whatever its firmware says:// what it costs, it costs here, never a session. The Wi-Fi device beside them (services/wifiProcess.ts),- // on a link of its own: its receipts, its streams and its Store preparations.- devices: { services: ['devices', 'wifi'], heapLimitMiB: 256, rssLimitMiB: 512 },+ // on a link of its own: its receipts, its streams and its Store preparations. Started only once there is+ // a device, or a request for one (core/devicesWake.ts): about 72 MiB at idle, which a computer with none+ // never pays.+ devices: { services: ['devices', 'wifi'], heapLimitMiB: 256, rssLimitMiB: 512, onDemand: true, askedSince: 4 },}/** Every service this build can run outside the core's process: what `HARNESSD_SERVICES` names. */@@ -426,8 +436,8 @@ export function servicesTheMasterRuns(env: NodeJS.ProcessEnv, known: readonly st* opt-in. Each process hosts the services named of its own, and is not started for none. A process* named as one of its services is named whole: `viewers` runs the viewers' process, the Store beside* them, as it ran the viewers' before the Store joined it. `HARNESSD_SERVICE_HEAP_LIMIT_MIB` gives every- * one the same heap limit instead (tests, support). An experiment's process (`onDemand`) waits for the core- * to ask for it; named in `HARNESSD_SERVICES`, it starts with the others.+ * one the same heap limit instead (tests, support). A process on demand (`onDemand`: an experiment's, the+ * devices') waits for the core to ask for it; named in `HARNESSD_SERVICES`, it starts with the others.*/export function serviceSpecs(env: NodeJS.ProcessEnv, hosts: Readonly<Record<string, ServiceHostSpec>>): ServiceSpec[] {const named = env.HARNESSD_SERVICES === undefined ? null@@ -435,7 +445,7 @@ export function serviceSpecs(env: NodeJS.ProcessEnv, hosts: Readonly<Record<striconst heap = Number(env.HARNESSD_SERVICE_HEAP_LIMIT_MIB)return Object.entries(hosts).flatMap(([name, host]) => {const services = host.services.filter((service) => !named || named.has(service) || named.has(name))- // An experiment named outright runs from the start, as every service named does (a test, support).+ // A process on demand named outright runs from the start, as every service named does (a test, support).const onDemand = host.onDemand && !named ? { onDemand: true } : {}const { onDemand: _given, ...rest } = hostreturn services.length ? [{ name, ...rest, services, ...onDemand, ...(Number.isInteger(heap) && heap > 0 ? { heapLimitMiB: heap } : {}) }] : []diff --git a/cli/src/harnessd/supervisor.spec.ts b/cli/src/harnessd/supervisor.spec.tsindex 26003588c..f918cf657 100644--- a/cli/src/harnessd/supervisor.spec.ts+++ b/cli/src/harnessd/supervisor.spec.ts@@ -116,17 +116,21 @@ describe('Supervisor', () => {expect(statuses).toHaveLength(3)})- it('starts the experiment a bound core asks for, and every experiment for a core too old to ask', () => {- const wanted: Array<string | null> = []- make({}, { want: (service) => wanted.push(service) }).start()+ it('starts the process a bound core asks for, and those a core too old to ask for them never would', () => {+ const wanted: string[] = []+ const unasked: number[] = []+ make({}, { want: (service) => wanted.push(service), unasked: (protocol) => unasked.push(protocol) }).start()core().say({ type: 'harnessd:want', service: 'orchestrator' })expect(wanted).toEqual([])core().bind()core().say({ type: 'harnessd:want', service: 'orchestrator' })expect(wanted).toEqual(['orchestrator'])+ expect(unasked).toEqual([])crash()core().bind(2)- expect(wanted).toEqual(['orchestrator', null])+ crash()+ core().bind(3)+ expect(unasked).toEqual([2, 3])// A master with no services to start ignores both.make().start()core().bind(2)diff --git a/cli/src/harnessd/supervisor.ts b/cli/src/harnessd/supervisor.tsindex f7c582c44..e34e0c698 100644--- a/cli/src/harnessd/supervisor.ts+++ b/cli/src/harnessd/supervisor.ts@@ -63,12 +63,13 @@ export interface SupervisorDeps {* since it started. Left out, such an exit is the update failing, as it was before.*/bundle?(): string | null+ /** Start the process that runs [service] on demand (./services.ts `ServiceSupervisor.want`): the core asked for it. */+ want?(service: string): void/**- * Start the experiment's process that runs [service] (./services.ts `ServiceSupervisor.want`): the core- * asked for it. Null for every experiment: a core from before `want` (protocol 2 or less) never asks,- * and expects each service it routes to run, as every one did.+ * The core bound speaking an older protocol than this master (./services.ts `ServiceSupervisor.unasked`): it+ * never asks for a process that became on demand after its protocol, and expects it to run, as it did.*/- want?(service: string | null): void+ unasked?(protocol: number): void}/**@@ -371,7 +372,7 @@ export class Supervisor {this.clearTimer('bindTimer')if (!this.claimed) { this.deps.claimPidFile(); this.claimed = true }this.deps.log(`[harnessd] core bound (pid ${core.pid ?? '?'}, protocol ${message.protocol})`)- if (message.protocol < 3) this.deps.want?.(null)+ if (message.protocol < HARNESSD_PROTOCOL) this.deps.unasked?.(message.protocol)this.watchHeartbeat()// A core from before `ready` is running once bound, and its update is judged from there.const readiness = message.protocol >= 2diff --git a/cli/src/services/AGENTS.md b/cli/src/services/AGENTS.mdindex 2539ffcac..2042265b1 100644--- a/cli/src/services/AGENTS.md+++ b/cli/src/services/AGENTS.md@@ -131,6 +131,9 @@ way back).their requests on in order and checks where each answer goes, and keeps what it reads in line of theservice (who said hello, which transcripts and streams it follows, the focus revision) from what theservice tells it (src/core/wifi.ts). `e2e/wifiDevice.e2e.ts` proves it, with a fake device on the relay.+ Their process runs only once there is a device (`onDemand`, asked for since protocol 4): a dial's port in+ /dev, a paired Wi-Fi device or its session, or a request for them (src/core/devicesWake.ts);+ what a Wi-Fi device sends while it starts is held by the core. `e2e/devicesOnDemand.e2e.ts` proves it.- State built from every change must get every change exactly once. The teams show how(`src/services/teamsProcess.ts`, `src/core/teamsLink.ts`): the core numbers each change and keeps ituntil the process acknowledges it; on each connection the process says what it applied and getsdiff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.mdindex 65abfa591..2c46d093d 100644--- a/docs/design/2026-10-03-harnessd.md+++ b/docs/design/2026-10-03-harnessd.md@@ -310,6 +310,17 @@ The steps, each a pull request with its end-to-end proof:resync (the documented way back after a daemon restart) and served on without a new hello; its receiptsare in its journal. Proven by e2e/wifiDevice.e2e.ts (a fake device through the fake relay, beside twofake dials) and by e2e/deviceCompat.e2e.ts, which hears the same answers and events from main's build.+- **The devices on demand (done, 6 October).** The devices' process starts only once there is a device:+ about 72 MiB at idle that a computer with none no longer pays. The core asks the master for it+ (`harnessd:want`, protocol 4; a core of protocol 3 or older never asks, so the master starts it as that+ core binds) when a dial's port is in /dev (a USB modem's node, looked for every two seconds, the dial's+ own scan's cadence, with no `ioreg` and no open), when a Wi-Fi device is paired (the gateway's pairings or+ its direct links, read once at start), when a Wi-Fi device's session comes through the relay or a pairing begins, and when a request needs it+ (the Devices tab, ⌘K), which waits for it as an experiment's does (core/devicesWake.ts). What a Wi-Fi+ device sends meanwhile is held, in order, and handed on once its service has been resumed. Once+ started it stays, as every service does; stopping it when the last device leaves was measured and left+ out (the PR says why). `HARNESSD_SERVICES=devices` still starts it with the daemon, and `none` runs it+ in the core. Proven by e2e/devicesOnDemand.e2e.ts and e2e/wifiDevice.e2e.ts.### Modelsdiff --git a/docs/design/2026-10-06-core-boundary-next.md b/docs/design/2026-10-06-core-boundary-next.mdindex c9ab7fb2d..0640e952d 100644--- a/docs/design/2026-10-06-core-boundary-next.md+++ b/docs/design/2026-10-06-core-boundary-next.md@@ -14,7 +14,7 @@> | 6 The Store into the viewers' process | #879 | |> | 7 Models in its own process | #896 | Found: routed requests were cut off at 30 s (`LONG_ANSWERS`) |> | 8 The experimental host, move only | #921, #924, #932, #933 | On-demand processes per experiment: orchestrator, Tab collaboration, Share |-> | 9 Devices in their own process | #918, #928, #929 | Per-device guards in one devices process; the Wi-Fi device on the gateway |+> | 9 Devices in their own process | #918, #928, #929, #946 | Per-device guards in one devices process; the Wi-Fi device on the gateway; the process on demand, once there is a device |> | 10 The relay: the gateway process | #899, #905, #911 | |> | 11 The unsupervised handoff | #895 | `harness start -f` runs the master; the updater leaves the core in #925 |>@@ -441,6 +441,11 @@ app sends one of its requests. Today the dial's USB watch is itself the devices'spawn every 2 s); the cheap watch would run in the edge host and ask the master to start the devices.This is a decision for the owner (below); without it, both run always, at 350 MiB in all.+*Decided and done (6 October):* the devices are on demand. The cheap watch runs in the core+(core/devicesWake.ts: a listing of /dev every two seconds, no `ioreg`, no open), with a paired Wi-Fi+device, its session, a pairing and the Devices tab's and ⌘K's requests. Idle with no device, the+daemon's processes went from 660–669 to 592–594 MiB RSS.+## The orderEach step is one pull request that keeps every client working, with its end-to-end proof, and lowers