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
Fix round-device pane focus delays across machines (#1103)
Deed81fea66b526
diff --git a/cli/src/cable/cableSession.spec.ts b/cli/src/cable/cableSession.spec.tsindex 2d8cf656a..7e9539ea8 100644--- a/cli/src/cable/cableSession.spec.ts+++ b/cli/src/cable/cableSession.spec.ts@@ -1196,7 +1196,38 @@ describe('cable session', () => {await session.stop()})- it('coalesces quick app selections so the newest one focuses last', async () => {+ it('focuses a remote pane before its machine connection answers', async () => {+ let selected = 'mac-local'+ let finishSelection!: () => void+ const gate = new Promise<void>(resolve => { finishSelection = resolve })+ const selectMachine = vi.fn(async (machineId: string) => {+ await gate+ selected = machineId+ return { ok: true as const }+ })+ const { session, port } = await connect(makeHost({+ selectedMachine: () => selected,+ selectMachine,+ listAgents: async () => [...AGENTS, { id: 'r1', name: 'Remote pane', machineId: 'remote-machine' }],+ }))+ let following: Promise<void> | undefined+ try {+ port.say({ t: 'hello', product: 'harness', mac: 'aa:bb' })+ await settle()+ port.sent.length = 0+ following = session.followApp('remote-machine', 'r1')+ await settle()+ expect(selectMachine).toHaveBeenCalledWith('remote-machine')+ expect(selected).toBe('mac-local')+ expect(port.sent.filter(m => m.t === 'focus')).toEqual([{ t: 'focus', agentId: 'r1' }])+ } finally {+ finishSelection()+ await following+ await session.stop()+ }+ })++ it('follows the newest pane without waiting for an older machine selection', async () => {let selected = 'mac-local'let finishFirstSelection!: () => voidconst firstSelectionGate = new Promise<void>((resolve) => { finishFirstSelection = resolve })@@ -1217,13 +1248,21 @@ describe('cable session', () => {const oldSelection = session.followApp('remote-machine', 'r1')await settle()+ port.sent.length = 0const newestSelection = session.followApp('mac-local', 'a2')- finishFirstSelection()- await Promise.all([oldSelection, newestSelection])-- expect(port.sent.filter((m) => m.t === 'focus').map((m) => m.agentId)).toEqual(['a2'])- expect(selected).toBe('mac-local')- await session.stop()+ try {+ await settle()+ expect(port.sent.filter((m) => m.t === 'focus').map((m) => m.agentId)).toEqual(['a2'])+ finishFirstSelection()+ await Promise.all([oldSelection, newestSelection])+ expect(port.sent.filter((m) => m.t === 'focus').map((m) => m.agentId)).toEqual(['a2'])+ expect(port.sent.filter((m) => m.t === 'machine.selected').map((m) => m.machineId)).toEqual(['mac-local'])+ expect(selected).toBe('mac-local')+ } finally {+ finishFirstSelection()+ await Promise.all([oldSelection, newestSelection])+ await session.stop()+ }})it.each([{ selections: ['a2'] }, { selections: ['a1', 'a2'] }])('delivers the latest focus when $selections supersedes an unsent selection', async ({ selections }) => {@@ -1243,6 +1282,71 @@ describe('cable session', () => {} finally { await session.stop() }})+ it('settles the machine wheel when a newer pane uses the same pending connection', async () => {+ let selected = 'mac-local'+ let finish!: () => void+ const gate = new Promise<void>(resolve => { finish = resolve })+ const selectMachine = vi.fn(async (machineId: string) => {+ await gate+ selected = machineId+ return { ok: true as const }+ })+ const { session, port } = await connect(makeHost({ selectedMachine: () => selected, selectMachine }))+ const pending: Promise<void>[] = []+ try {+ port.say({ t: 'hello', product: 'harness', mac: 'aa:bb' })+ await settle()+ pending.push(session.followApp('remote-machine', 'r1'))+ await settle()+ port.sent.length = 0+ pending.push(session.followApp('remote-machine', 'r2'))+ await settle()+ expect(port.sent.filter(m => m.t === 'focus')).toEqual([{ t: 'focus', agentId: 'r2' }])+ finish()+ await Promise.all(pending)+ expect(selectMachine).toHaveBeenCalledTimes(1)+ expect(port.sent.filter(m => m.t === 'machines.end').at(-1)).toMatchObject({ selected: 'remote-machine' })+ expect(port.sent.filter(m => m.t === 'focus')).toEqual([{ t: 'focus', agentId: 'r2' }])+ } finally { finish(); await Promise.all(pending); await session.stop() }+ })++ it.each(['refused', 'throws', 'disconnected'])(+ 'keeps pane focus independent when a remote attachment is %s', async outcome => {+ let finish!: () => void+ const gate = new Promise<void>(resolve => { finish = resolve })+ const log = vi.fn()+ const { session, port } = await connect(makeHost({+ log,+ selectMachine: async () => {+ await gate+ if (outcome === 'throws') throw new Error('connection lost')+ if (outcome === 'refused') return { ok: false, code: 'UNREACHABLE', message: 'Remote unavailable' }+ return { ok: true }+ },+ }))+ let following: Promise<void> | undefined+ try {+ port.say({ t: 'hello', product: 'harness', mac: 'aa:bb' })+ await settle()+ port.sent.length = 0+ following = session.followApp('remote-machine', 'r1')+ await settle()+ expect(port.sent.filter(m => m.t === 'focus')).toEqual([{ t: 'focus', agentId: 'r1' }])+ if (outcome === 'disconnected') await port.close()+ finish()+ await following+ if (outcome === 'disconnected') {+ expect(port.sent).toEqual([{ t: 'focus', agentId: 'r1' }])+ } else {+ if (outcome === 'refused') expect(port.sent).toContainEqual(expect.objectContaining({ t: 'machine.error', code: 'UNREACHABLE' }))+ else expect(log).toHaveBeenCalledWith('cable: could not follow app machine (connection lost)')+ await session.followApp('mac-local', 'a2')+ expect(port.sent.filter(m => m.t === 'focus').at(-1)).toEqual({ t: 'focus', agentId: 'a2' })+ }+ } finally { finish(); await following; await session.stop() }+ },+ )+it('forwards a whole stroke, including the reports that carry no travel', async () => {// The ends of a stroke are the point of the message, not padding around it: a `down` with nothing in// it is what stops a fling still running on the far side, and an `up` with nothing in it is a fingerdiff --git a/cli/src/cable/cableSession.ts b/cli/src/cable/cableSession.tsindex 60c003a20..28e99337d 100644--- a/cli/src/cable/cableSession.ts+++ b/cli/src/cable/cableSession.ts@@ -550,11 +550,10 @@ export class CableSession {private desiredFocus = ''/** A superseded list refresh can record the selection without ever writing its focus frame. */private appFocusUnsent = false- /**- * App-driven machine/focus changes are one transaction. Without this queue, two quick desktop clicks- * can interleave their machine lists and let the older click focus last.- */+ /** Keep list-before-focus writes ordered, coalescing superseded desktop selections. */private appFocusTail: Promise<void> = Promise.resolve()+ /** A remote attachment can take seconds. It must not hold the next pane's focus behind it. */+ private appMachineTail: Promise<void> = Promise.resolve()private appFocusGeneration = 0/** When the app-driven switch began — see APP_SWITCH_REPAINT_MS. */private drivingSince = 0@@ -2278,7 +2277,7 @@ export class CableSession {* until its own deadline, which reads as a hang rather than as "done, nothing changed" — the same rule* `models.list` follows.*/- private async selectMachine(machineId: string): Promise<void> {+ private async selectMachine(machineId: string, isCurrent = () => true): Promise<void> {if (!machineId) returnif (machineId === this.host.selectedMachine()) {await this.send({ t: 'machine.selected', machineId })@@ -2287,6 +2286,9 @@ export class CableSession {return}const result = await this.host.selectMachine(machineId)+ // An app selection may have moved again, or its cable closed, during the remote attachment.+ // The next queued selection corrects the host; never publish this obsolete acknowledgement.+ if (!isCurrent()) returnif (!result.ok) {this.log(`cable: machine.select ${machineId} refused (${result.code})`)await this.send({ t: 'machine.error', machineId, code: result.code, message: result.message })@@ -2518,9 +2520,10 @@ export class CableSession {* The record is one field written by both sides (see desiredFocus). It used to be two, and the ring* closed through the gap between them.*- * The machine comes first when it differs. Sending `focus` for an agent on a machine the dial is not on- * would name an id its list has never heard of, and the tile it would have to move to does not exist- * yet — selectMachine is what streams that list.+ * The list comes first, but attaching to the machine does not. The dial now holds the active tab's+ * panes across all machines. Waiting for the separate remote attachment held even a newer LOCAL+ * selection behind the network, leaving the round unit on the old pane. Update the machine wheel+ * separately, still in order, so a slow attachment cannot block the selected pane.*/async followApp(machineId: string, agentId: string): Promise<void> {if (!this.link?.isOpen || this.greetedMac === null) return@@ -2533,15 +2536,9 @@ export class CableSession {this.drivingSince = Date.now()this.drivingAppFocus = truetry {- if (machineId && machineId !== this.host.selectedMachine()) {- this.log(`cable: following the app to machine ${machineId}`)- await this.selectMachine(machineId)- }- // A newer selection can arrive while the remote machine RPC/list push is in flight. Never let- // this older transaction focus after it finishes.// Already where the window is: nothing to command. The record is right either way — this is the// window FOLLOWING the dial, and the dial's own report wrote it.- if (generation !== this.appFocusGeneration || (agentId === this.desiredFocus && !this.appFocusUnsent)) return+ if (agentId === this.desiredFocus && !this.appFocusUnsent) return// Said before the frame goes out, not after: the frame itself succeeds either way.const unknown = this.host.knows?.(agentId) === falsethis.log(`cable: following the app to agent ${agentId}${unknown ? ' — NOT in this daemon\'s list, the dial has no tile for it' : ''}`)@@ -2572,10 +2569,26 @@ export class CableSession {// Keep the queue usable after one transport failure. The local websocket intentionally// fire-and-forgets followApp, so the session log is the only place an unexpected rejection would// otherwise be visible; consume it here rather than creating an unhandled rejection.- this.appFocusTail = task.catch((err) => {+ const focused = this.appFocusTail = task.catch((err) => {this.log(`cable: could not follow app focus (${(err as Error).message})`)})- await this.appFocusTail+ const machineTask = this.appMachineTail.then(async () => {+ await focused+ const isCurrent = () => generation === this.appFocusGeneration+ if (!isCurrent() || !machineId) return+ if (machineId === this.host.selectedMachine()) {+ // An older attachment to this same machine may have finished without acknowledging it,+ // because a newer pane superseded that request. The wheel still needs its selection.+ await this.syncMachines()+ return+ }+ this.log(`cable: following the app to machine ${machineId}`)+ await this.selectMachine(machineId, isCurrent)+ })+ const selected = this.appMachineTail = machineTask.catch((err) => {+ this.log(`cable: could not follow app machine (${(err as Error).message})`)+ })+ await Promise.all([focused, selected])}async toast(text: string): Promise<void> {await this.send({ t: 'toast', text })