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
refactor: remove agent count from machine fleet and cable communication protocol for consistency
0xken1538098b728c6d
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 78b705541..bde0ef877 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -906,8 +906,12 @@ export class BackendSocket {case 'agents_list': {const projects = await Promise.all(registry.active().map((s) => this.toProject(s)))// Ordered by creation time, oldest → newest — a stable tab order that doesn't reshuffle as- // sessions become active (createdAt = the session's registeredAt).- projects.sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt))+ // sessions become active (createdAt = the session's registeredAt). The id breaks a tie so the+ // order is TOTAL: without it two agents registered in the same millisecond fall through to array+ // position, which is Map insertion order and differs between daemon runs — the web, the app and+ // the dial would each show a different order for the same registry. `cableHost.listAgents`+ // sorts by the same rule; the two must stay identical.+ projects.sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt) || a.id.localeCompare(b.id))if (this.e2ee.sessionRole(connId) === 'device') {reply(type, requestId, { agents: projects.slice(0, DEVICE_AGENT_LIST_LIMIT).map(deviceAgentListItem) })returndiff --git a/cli/src/cable/cableHost.spec.ts b/cli/src/cable/cableHost.spec.tsnew file mode 100644index 000000000..8b5849d78--- /dev/null+++ b/cli/src/cable/cableHost.spec.ts@@ -0,0 +1,119 @@+// How the wheel's rows are composed: the local row, and the fleet's rows around it.+import { describe, expect, it, vi } from 'vitest'++import { DaemonCableHost, type CableHostWiring } from './cableHost.js'+import type { FleetMachine, MachineFleet } from './machineFleet.js'++const AGENTS: Array<{ agentId: string; registeredAt: number; active: boolean; engine: string }> = []+vi.mock('../lib/registry.js', () => ({+ registry: {+ list: () => AGENTS,+ active: () => AGENTS.filter((a) => a.active),+ },+ projectDisplayName: (s: { agentId: string }) => s.agentId,+}))++function wiring(over: Partial<CableHostWiring> = {}): CableHostWiring {+ return {+ machineName: () => 'MacbookPro.local',+ machineId: () => 'mine',+ computerId: () => 'abc-123',+ sendTurn: vi.fn(),+ stopTurn: vi.fn(),+ answer: vi.fn(),+ recent: () => [],+ log: vi.fn(),+ ...over,+ }+}++function fleetOf(machines: FleetMachine[]): MachineFleet {+ return {+ list: async () => ({ machines, source: 'backend' as const }),+ select: async () => {},+ release: vi.fn(),+ listAgents: async () => [],+ sendTurn: vi.fn(), stopTurn: vi.fn(), answer: vi.fn(), updateAgent: vi.fn(),+ listModels: async () => [],+ recentSummaries: async () => [],+ onEvent: () => () => {},+ }+}++const REMOTE: FleetMachine = { machineId: 'other', name: 'office-imac', state: 'ready', authMode: 'remote' }++describe('DaemonCableHost.listMachines', () => {+ it('names the local row after the machine, and marks it local', async () => {+ // The row carries the machine's name; what identifies it as the cabled one is the `local` flag, which+ // the dial turns into the second line. Two facts, one field each.+ const host = new DaemonCableHost(wiring(), fleetOf([REMOTE]))+ const { machines } = await host.listMachines()+ expect(machines[0]).toMatchObject({ id: 'mine', name: 'MacbookPro.local', local: true, state: 'ready' })+ })++ it('puts the local row first and keeps the fleet’s rows after it', async () => {+ const host = new DaemonCableHost(wiring(), fleetOf([REMOTE]))+ const { machines } = await host.listMachines()+ expect(machines.map((m) => m.id)).toEqual(['mine', 'other'])+ expect(machines[1]).toMatchObject({ name: 'office-imac', local: false })+ })++ it('never lists this computer twice, and a rename on the account does not touch its label', async () => {+ // `GET /api/machines` contains this computer too. Letting that row through would put the same machine+ // on the wheel under two names, and the ✓ could only ever mark one of them.+ const dupe: FleetMachine = { machineId: 'mine', name: 'renamed-in-the-web-ui', state: 'offline', authMode: 'remote' }+ const host = new DaemonCableHost(wiring(), fleetOf([dupe, REMOTE]))+ const { machines } = await host.listMachines()+ expect(machines.map((m) => m.id)).toEqual(['mine', 'other'])+ expect(machines[0]).toMatchObject({ name: 'MacbookPro.local', state: 'ready' })+ })++ it('shows one row and says why when there is no fleet at all', async () => {+ const host = new DaemonCableHost(wiring())+ const { machines, source } = await host.listMachines()+ expect(source).toBe('signed-out')+ expect(machines).toHaveLength(1)+ expect(machines[0].local).toBe(true)+ })++ it('falls back to a cable: id when the daemon has never resolved a machineId', async () => {+ // Signed out, or offline before the first resolve. The row must still exist and still be selectable —+ // it is the one machine that is certainly reachable.+ const host = new DaemonCableHost(wiring({ machineId: () => '' }))+ const { machines } = await host.listMachines()+ expect(machines[0].id).toBe('cable:abc-123')+ expect(host.isLocalSelected()).toBe(true)+ })+})++describe('DaemonCableHost.listAgents', () => {+ const set = (rows: Array<{ agentId: string; registeredAt: number; active?: boolean }>): void => {+ AGENTS.length = 0+ for (const r of rows) AGENTS.push({ engine: 'claude', active: true, ...r })+ }++ it('lists only ACTIVE agents — the same set the web and the app are given', async () => {+ // Two surfaces reading one registry must not disagree about what is on it. A dead agent holding a tile+ // on the dial and nowhere else is a tile that cannot be driven and cannot be explained.+ set([{ agentId: 'a', registeredAt: 1 }, { agentId: 'b', registeredAt: 2, active: false }])+ const host = new DaemonCableHost(wiring())+ expect((await host.listAgents()).map((a) => a.id)).toEqual(['a'])+ })++ it('orders oldest first', async () => {+ set([{ agentId: 'new', registeredAt: 30 }, { agentId: 'old', registeredAt: 10 }, { agentId: 'mid', registeredAt: 20 }])+ const host = new DaemonCableHost(wiring())+ expect((await host.listAgents()).map((a) => a.id)).toEqual(['old', 'mid', 'new'])+ })++ it('breaks a tie by id, so the order never falls through to insertion order', async () => {+ // Without the tie-break the winner is whichever the Map happens to hold first — which differs between+ // daemon runs, so the web, the app and the dial would each show a different order for one registry.+ set([{ agentId: 'zz', registeredAt: 5 }, { agentId: 'aa', registeredAt: 5 }])+ const host = new DaemonCableHost(wiring())+ expect((await host.listAgents()).map((a) => a.id)).toEqual(['aa', 'zz'])++ set([{ agentId: 'aa', registeredAt: 5 }, { agentId: 'zz', registeredAt: 5 }])+ expect((await new DaemonCableHost(wiring()).listAgents()).map((a) => a.id)).toEqual(['aa', 'zz'])+ })+})diff --git a/cli/src/cable/cableHost.ts b/cli/src/cable/cableHost.tsindex 9e23ec132..5a57bcc05 100644--- a/cli/src/cable/cableHost.ts+++ b/cli/src/cable/cableHost.ts@@ -31,8 +31,6 @@ export interface CableHostWiring {machineId: () => string/** A stable id for this computer, used to name the local row when there is no machineId yet. */computerId: () => string- /** How many agents this computer is running — the local row's count, free to read. */- localAgentCount: () => number/** Deliver text into an agent. The SAME path the web and the WiFi device use — see cli.ts. */sendTurn: (agentId: string, text: string) => voidstopTurn: (agentId: string) => void@@ -59,7 +57,7 @@ function localFallbackId(computerId: string): string {/** A fleet row as the wire carries it. `authMode` does not travel: the dial has no use for the word, and* `remote` is just `!local`, which the row already says. */function toCableMachine(m: FleetMachine): CableMachine {- return { id: m.machineId, name: m.name, state: m.state, local: false, agents: m.agentCount }+ return { id: m.machineId, name: m.name, state: m.state, local: false }}/** Split `runtime-v1:<sid>:<engine>:<model>@<effort>` back into the two words the dial's chips show. */@@ -106,19 +104,21 @@ export class DaemonCableHost implements CableHost {async listMachines(): Promise<{ machines: CableMachine[]; source: CableMachineSource }> {const local: CableMachine = {id: this.localId(),+ // The machine's own name. What makes this row recognisable as the cabled one is its second line,+ // which the dial writes — see machine_meta_line.name: this.wiring.machineName(),// Always ready: the cable IS the evidence. Nothing else on this list can say that about itself.state: 'ready',local: true,- agents: this.wiring.localAgentCount(),}if (!this.fleet) return { machines: [local], source: 'signed-out' }const { machines, source } = await this.fleet.list()const rows: CableMachine[] = [local]for (const m of machines) {- // The backend list contains THIS computer too. Keep its id and name — they are the canonical ones —- // but the liveness and the agent count come from here, where they are facts rather than a snapshot.- if (m.machineId === local.id) { local.name = m.name || local.name; continue }+ // The backend list contains THIS computer too. Dropped rather than rendered: the same machine on+ // the wheel twice, under two names, with the ✓ able to mark only one of them. Its name is already+ // here anyway — MACHINE_NAME_FILE is mirrored from the backend on every connect.+ if (m.machineId === local.id) continuerows.push(toCableMachine(m))}return { machines: rows, source }@@ -160,10 +160,14 @@ export class DaemonCableHost implements CableHost {async listAgents(): Promise<CableAgent[]> {if (!this.isLocalSelected()) return this.fleet!.listAgents(this.selectedMachine())- const sessions = registry.list()- // Oldest → newest: a stable order that does not reshuffle as agents become active, which matters more- // on a carousel than on a list — a tile that moves under a swipe is a tile you cannot aim at.- sessions.sort((a, b) => a.registeredAt - b.registeredAt)+ // `active()`, not `list()` — the SAME set `agents_list` answers the web and the desktop app with. They+ // read one registry and must not disagree about what is on it: a dead agent holding a tile on the dial+ // and nowhere else is a tile that cannot be driven and cannot be explained.+ const sessions = registry.active()+ // Oldest → newest, and TOTAL: the id breaks a tie so the order cannot fall through to array position,+ // which is Map insertion order and differs between daemon runs. Both producers sort identically, so+ // the dial and the app cannot drift apart while reading the same registry.+ sessions.sort((a, b) => a.registeredAt - b.registeredAt || a.agentId.localeCompare(b.agentId))return sessions.map((s) => ({id: s.agentId,name: projectDisplayName(s),diff --git a/cli/src/cable/cableSession.spec.ts b/cli/src/cable/cableSession.spec.tsindex bd97139e1..a4a0bc497 100644--- a/cli/src/cable/cableSession.spec.ts+++ b/cli/src/cable/cableSession.spec.ts@@ -62,7 +62,7 @@ class LoopbackPort implements CablePort {}}-const LOCAL_ROW: CableMachine = { id: 'mac-local', name: 'MacBook Pro', state: 'ready', local: true, agents: 2 }+const LOCAL_ROW: CableMachine = { id: 'mac-local', name: 'MacBook Pro', state: 'ready', local: true }const AGENTS: CableAgent[] = [{ id: 'a1', name: 'Fix login screen', engine: 'claude' },@@ -409,7 +409,7 @@ describe('cable session', () => {})it('acks a select, then pushes THAT machine\'s agents', async () => {- const other: CableMachine = { id: 'm2', name: 'office-imac', state: 'ready', local: false, agents: 3 }+ const other: CableMachine = { id: 'm2', name: 'office-imac', state: 'ready', local: false }const remote: CableAgent[] = [{ id: 'r1', name: 'api refactor', engine: 'codex' }]let selected = 'mac-local'const host = makeHost({@@ -438,7 +438,7 @@ describe('cable session', () => {let selected = 'mac-local'const host = makeHost({listMachines: async () => ({- machines: [LOCAL_ROW, { id: 'm2', name: 'twin', state: 'ready' as const, local: false, agents: 2 }],+ machines: [LOCAL_ROW, { id: 'm2', name: 'twin', state: 'ready' as const, local: false }],source: 'backend' as const,}),selectedMachine: () => selected,@@ -516,7 +516,6 @@ describe('cable session', () => {name: 'x'.repeat(39),state: 'ready' as const,local: i === 0,- agents: 99,}))const host = makeHost({ listMachines: async () => ({ machines: many, source: 'backend' as const }) })const { session, port } = await connect(host)@@ -558,7 +557,7 @@ describe('cable session', () => {const rows: CableMachine[] = [LOCAL_ROW,- { id: 'm2', name: 'office-imac', state: 'ready', local: false, agents: 3 },+ { id: 'm2', name: 'office-imac', state: 'ready', local: false },]const host = makeHost({ listMachines: async () => ({ machines: rows, source: 'backend' as const }) })let port!: SlowPortdiff --git a/cli/src/cable/cableSession.ts b/cli/src/cable/cableSession.tsindex 24b205d82..5b960c8bf 100644--- a/cli/src/cable/cableSession.ts+++ b/cli/src/cable/cableSession.ts@@ -66,8 +66,6 @@ export interface CableMachine {state: 'ready' | 'offline' | 'unknown' | 'needs-link'/** Exactly one row carries this: the computer at the other end of the cable. */local: boolean- /** −1 when unknown. Sent only when it is free to know — never worth a round trip per machine. */- agents: number}/** Why the list is as short as it is. The dial renders this, instead of drawing an empty wheel. */@@ -691,7 +689,7 @@ export class CableSession {* order, so two lists that differ only by a swap are a real change. */private static machinesKey(machines: CableMachine[], source: string, selected: string): string {return `${source}|${selected}|${machines- .map((m) => `${m.id}:${m.name}:${m.state}:${m.agents}:${m.local ? 1 : 0}`)+ .map((m) => `${m.id}:${m.name}:${m.state}:${m.local ? 1 : 0}`).join('|')}`}@@ -717,7 +715,7 @@ export class CableSession {await this.send({ t: 'machines.begin' })for (const m of machines) {- await this.send({ t: 'machine', id: m.id, name: m.name, state: m.state, local: m.local, agents: m.agents })+ await this.send({ t: 'machine', id: m.id, name: m.name, state: m.state, local: m.local })}await this.send({ t: 'machines.end', selected, source })}@@ -755,14 +753,7 @@ export class CableSession {/** One row changed — liveness, a rename, a count. Cheaper than re-streaming the wheel. */async machineState(machine: CableMachine): Promise<void> {- await this.send({- t: 'machine.updated',- id: machine.id,- name: machine.name,- state: machine.state,- local: machine.local,- agents: machine.agents,- })+ await this.send({ t: 'machine.updated', id: machine.id, name: machine.name, state: machine.state, local: machine.local })}// Turn state is UNSOLICITED: the daemon reports every turn in every agent, including ones started at thediff --git a/cli/src/cable/machineFleet.ts b/cli/src/cable/machineFleet.tsindex cd639e71d..e03158db1 100644--- a/cli/src/cable/machineFleet.ts+++ b/cli/src/cable/machineFleet.ts@@ -17,7 +17,6 @@ export interface FleetMachine {/** `ready` = reachable now · `offline` = known but not reachable · `unknown` = the list itself is stale* · `needs-link` = reachable, but this daemon holds no pinned key for it, so nothing can be read. */state: 'ready' | 'offline' | 'unknown' | 'needs-link'- agentCount: numberauthMode: 'self' | 'managed' | 'remote' | 'provider'}@@ -33,7 +32,7 @@ export class FleetError extends Error {export type FleetEvent =| { machineId: string; kind: 'processing' | 'done' | 'summary' | 'error'; agentId: string; text: string; recap: string }| { machineId: string; kind: 'question'; agentId: string; requestId: string; questions: unknown }- | { machineId: string; kind: 'state'; state: FleetMachine['state']; agentCount?: number }+ | { machineId: string; kind: 'state'; state: FleetMachine['state'] }/*** Everything the dial needs from a machine that is NOT this computer.diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 2077b21cf..6c170b9a1 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -2855,7 +2855,6 @@ async function runForeground(session: AuthSession): Promise<void> {machineName: () => { try { return readFileSync(MACHINE_NAME_FILE, 'utf8').trim() || 'This machine' } catch { return 'This machine' } },machineId: () => backend.machineId,computerId: () => computerId(),- localAgentCount: () => registry.list().length,// The SAME handlers the backend socket drives, called directly rather than reimplemented: the// slash-command adaptation, the turn bookkeeping and the question plumbing all live in them.sendTurn: (agentId, text) => backend.onMessage?.(agentId, text),diff --git a/cli/src/device/deviceFleet.spec.ts b/cli/src/device/deviceFleet.spec.tsindex 20c30a042..0221847df 100644--- a/cli/src/device/deviceFleet.spec.ts+++ b/cli/src/device/deviceFleet.spec.ts@@ -13,7 +13,7 @@ type Listed = { machineId: string; name: string; state: string; agentCount: numbfunction listOf(rows: Partial<Listed>[]): Listed[] {return rows.map((r) => ({- machineId: 'm1', name: 'office-imac', state: 'ready', agentCount: 2, authMode: 'remote', local: false, ...r,+ machineId: 'm1', name: 'office-imac', state: 'ready', authMode: 'remote', local: false, ...r,})) as Listed[]}diff --git a/cli/src/device/machineList.spec.ts b/cli/src/device/machineList.spec.tsindex 09d268a76..ee9002f32 100644--- a/cli/src/device/machineList.spec.ts+++ b/cli/src/device/machineList.spec.ts@@ -14,7 +14,7 @@ const DIR = (): string => mkdtempSync(join(tmpdir(), 'machines-'))/** The shape `GET /api/machines` actually answers with (MachineService.toOwner). */function row(over: Record<string, unknown> = {}): Record<string, unknown> {- return { machineId: 'm1', computerId: 'aabbccdd', name: 'office-imac', status: 'running', agentCount: 3, authMode: 'remote', ...over }+ return { machineId: 'm1', computerId: 'aabbccdd', name: 'office-imac', status: 'running', authMode: 'remote', ...over }}function ok(machines: Record<string, unknown>[]) {diff --git a/cli/src/device/machineList.ts b/cli/src/device/machineList.tsindex 5fc67500e..5cc3f8974 100644--- a/cli/src/device/machineList.ts+++ b/cli/src/device/machineList.ts@@ -20,7 +20,6 @@ interface OwnerMachine {name?: unknownhostname?: unknownstatus?: unknown- agentCount?: unknownauthMode?: unknown}@@ -111,7 +110,6 @@ export class MachineListCache {machineId,name,state: stateOf(typeof r.status === 'string' ? r.status : ''),- agentCount: typeof r.agentCount === 'number' ? r.agentCount : -1,authMode,// Derived, never declared — the same rule the desktop app follows.local: sameComputer(typeof r.computerId === 'string' ? r.computerId : '', mine),