Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(cable): bring the dial with the window when the app opens an agent

0xdiego250b7ce6351d95
diff --git a/cli/src/cable/cableSession.spec.ts b/cli/src/cable/cableSession.spec.ts
index 2fc66335e..695cbda98 100644
--- a/cli/src/cable/cableSession.spec.ts
+++ b/cli/src/cable/cableSession.spec.ts
@@ -175,6 +175,41 @@ describe('cable session', () => {
await session.stop()
})
+ it('brings the dial to the agent the window opened', async () => {
+ const host = makeHost()
+ const { session, port } = await connect(host)
+ port.say({ t: 'hello', mac: 'aa:bb' })
+ await settle()
+ port.sent.length = 0
+
+ await session.followApp('', 'a2')
+ await settle()
+
+ expect(port.sent.filter((m) => m.t === 'focus').map((m) => m.agentId)).toEqual(['a2'])
+ await session.stop()
+ })
+
+ it("never echoes the dial's own move back at it", async () => {
+ // THE RING: the dial's carousel reports `focus` up, the daemon hands that to the window, the window
+ // opens that agent's terminal, and a window opening a terminal is exactly what calls followApp. Left
+ // unguarded that answers the dial with the move it just made. It settles today only because the far
+ // end does not re-report a carousel that never moved — a property of its UI, not of this protocol.
+ const host = makeHost()
+ const { session, port } = await connect(host)
+ port.say({ t: 'hello', mac: 'aa:bb' })
+ await settle()
+
+ port.say({ t: 'focus', agentId: 'a2' }) // the dial moved itself
+ await settle()
+ port.sent.length = 0
+
+ await session.followApp('', 'a2') // the window caught up
+ await settle()
+
+ expect(port.sent.filter((m) => m.t === 'focus')).toEqual([])
+ 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 finger
diff --git a/cli/src/cable/cableSession.ts b/cli/src/cable/cableSession.ts
index dc95cb9af..605879b4c 100644
--- a/cli/src/cable/cableSession.ts
+++ b/cli/src/cable/cableSession.ts
@@ -175,6 +175,8 @@ export class CableSession {
/** What the dial was last told the agent list is. Empty = it has been told nothing. */
private lastAgentsKey = ''
+ /** The agent the dial is on, as last observed or commanded. See followApp() for why this exists. */
+ private dialFocus = ''
/** The same, for the machine wheel. Both reset together on any new port — see `tryOpen`. */
private lastMachinesKey = ''
/** Throttle for the machine list, which costs an HTTP read where the agent list costs a map lookup. */
@@ -450,7 +452,12 @@ export class CableSession {
return
}
case 'focus':
- if (str('agentId')) this.host.focus(str('agentId')!)
+ if (str('agentId')) {
+ // Remember where the dial IS, not just that it said so: followApp() compares against this to
+ // avoid echoing the dial's own move back at it.
+ this.dialFocus = str('agentId')!
+ this.host.focus(str('agentId')!)
+ }
return
case 'scroll': {
// Forwarded verbatim, including the reports carrying no travel: the two ends of a stroke are the
@@ -795,8 +802,34 @@ export class CableSession {
await this.send({ t: 'question', agentId, id, questions })
}
async focusAgent(agentId: string): Promise<void> {
+ this.dialFocus = agentId
await this.send({ t: 'focus', agentId })
}
+
+ /**
+ * The desktop window moved to an agent — bring the dial with it.
+ *
+ * ⚠️ THE `dialFocus` GUARD IS WHAT KEEPS THIS FROM OSCILLATING. The two screens drive each other: the
+ * dial's own carousel reports `focus` up, the daemon hands that to the app, the app opens that agent's
+ * terminal, and the app opening a terminal is exactly what calls this. Answering it with another `focus`
+ * closes the ring. It happens to settle today — the dial does not re-report a carousel that never moved
+ * — but that is a property of the far end's UI, not of this protocol, and it is one jittery settle away
+ * from a loop. So: say nothing when the dial is already there.
+ *
+ * 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.
+ */
+ async followApp(machineId: string, agentId: string): Promise<void> {
+ if (!this.link?.isOpen || this.greetedMac === null) return
+ if (machineId && machineId !== this.host.selectedMachine()) {
+ this.host.log(`cable: following the app to machine ${machineId}`)
+ await this.selectMachine(machineId)
+ }
+ if (!agentId || agentId === this.dialFocus) return
+ this.host.log(`cable: following the app to agent ${agentId}`)
+ await this.focusAgent(agentId)
+ }
async toast(text: string): Promise<void> {
await this.send({ t: 'toast', text })
}
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 5868f76f7..22e146959 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -2273,6 +2273,9 @@ async function runForeground(session: AuthSession): Promise<void> {
new MachinePeerStore(),
)
const localWsServer = attachLocalWsServer(hookServer, {
+ // The window and the dial are one desk: opening an agent in the app brings the dial to it, switching
+ // the dial's machine first when the app moved to another one.
+ onAppFocus: (machineId, agentId) => { void cableRef?.followApp(machineId, agentId) },
machineId: backend.machineId,
backend,
relayPool,
diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.ts
index e02dda023..e0b09e59a 100644
--- a/cli/src/localWsServer.ts
+++ b/cli/src/localWsServer.ts
@@ -26,6 +26,9 @@ export interface LocalWsServerOptions {
* backend's `/api/web-ws` — see lib/remoteRelay.ts. Omit to keep today's own-machine-only behavior. */
relayPool?: RemoteRelayPool
autonomousEnv?: string
+ /** The desktop app opened an agent's terminal — which agent, and on which machine. Lets the dial follow
+ * the window, so the two screens stay one desk. */
+ onAppFocus?: (machineId: string, agentId: string) => void
}
export interface LocalWsServer {
@@ -104,6 +107,9 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
wss.on('connection', (ws) => {
const connId = `local:${randomUUID()}`
let selected = false
+ // Which machine THIS connection is bound to. The app opens one local socket per machine, so it is
+ // fixed for the life of the connection — set once, beside `selected`.
+ let boundMachineId: string | null = null
let relay: RelaySession | null = null
let alive = true
let chain = Promise.resolve()
@@ -131,6 +137,7 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
const frame = jsonFrame(raw)
const payload = frame?.payload as Record<string, unknown> | undefined
const requestedMachineId = payload?.machineId
+ if (typeof requestedMachineId === 'string') boundMachineId = requestedMachineId
if (frame?.type !== 'machine_select'
|| typeof requestedMachineId !== 'string'
|| payload?.localProtocolVersion !== LOCAL_WS_PROTOCOL_VERSION) {
@@ -177,6 +184,18 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO
return
}
+ // THE APP MOVED — tell whoever wants to follow it, before the frame is dispatched either way.
+ // Sniffed here rather than in the backend socket because that path never sees a RELAYED machine's
+ // frames: those are forwarded upstream a few lines below and would be invisible, which is exactly
+ // the case that matters — the dial has to follow the window onto ANOTHER machine too.
+ if (!isBinary && options.onAppFocus && boundMachineId) {
+ const opened = jsonFrame(raw)
+ const agentId = (opened?.payload as Record<string, unknown> | undefined)?.agentId
+ if (opened?.type === 'terminal_open' && typeof agentId === 'string' && agentId) {
+ options.onAppFocus(boundMachineId, agentId)
+ }
+ }
+
if (relay) {
// The relay now terminates E2EE itself (lib/remoteRelay.ts) — every frame past this point is
// already plaintext going in and out, so binary frames use the SAME local wire format as this