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.

Dial: the focus first, the history behind it

0xdiego258dcd41659f91
diff --git a/cli/src/cable/cableSession.spec.ts b/cli/src/cable/cableSession.spec.ts
index deab531c9..047984cba 100644
--- a/cli/src/cable/cableSession.spec.ts
+++ b/cli/src/cable/cableSession.spec.ts
@@ -550,6 +550,34 @@ describe('cable session', () => {
await session.stop()
})
+ it('sends the focus without waiting for the new machine\'s history', async () => {
+ // THE BUG, measured on the desk: clicking from a local agent to a remote one left the dial on the old
+ // tile for 1.5 s. `selectMachine` awaited `pushRestores`, and a restore asks every agent's own machine
+ // what it was last doing — one cloud round trip per remote agent, in a serial loop, ahead of the one
+ // frame the person was actually waiting for. History is `restore: true`; it can land afterwards.
+ let releaseHistory!: () => void
+ const history = new Promise<void>((resolve) => { releaseHistory = resolve })
+ let selected = 'mac-local'
+ const host = makeHost({
+ selectedMachine: () => selected,
+ selectMachine: vi.fn(async (machineId: string) => { selected = machineId; return { ok: true as const } }),
+ listAgents: async () => [{ id: 'r1', name: 'Remote Claude', engine: 'claude' }],
+ recentSummaries: async () => { await history; return [{ recap: 'shipped it', text: 'shipped it' }] },
+ })
+ const { session, port } = await connect(host)
+ port.say({ t: 'hello', product: 'harness', mac: 'aa:bb' })
+ await settle()
+
+ await session.followApp('remote-machine', 'r1')
+
+ expect(port.sent.filter((m) => m.t === 'focus').map((m) => m.agentId)).toContain('r1')
+ expect(port.types()).not.toContain('summary') // nothing of the history has been waited on
+
+ releaseHistory()
+ await vi.waitFor(() => expect(port.types()).toContain('summary'))
+ await session.stop()
+ })
+
it('does not bounce a local app selection back to the previous remote agent', async () => {
let selected = 'remote-machine'
let finishSelection!: () => void
diff --git a/cli/src/cable/cableSession.ts b/cli/src/cable/cableSession.ts
index d220c9cfc..be0f3ed63 100644
--- a/cli/src/cable/cableSession.ts
+++ b/cli/src/cable/cableSession.ts
@@ -1155,18 +1155,35 @@ export class CableSession {
* rename would replay a week of recaps.
*/
async pushRestores(): Promise<void> {
- return this.queued(() => this.pushRestoresNow())
- }
-
- private async pushRestoresNow(): Promise<void> {
+ // READ FIRST, QUEUE SECOND, and the split is the whole point. A remote agent's history is a cloud
+ // round trip and there is one per agent; asking for them from INSIDE the push chain holds every frame
+ // behind them — including the `focus` the person who just clicked is waiting for. Measured on the
+ // desk: a click from a local agent to a remote one took 1.5 s, of which 0.7 s was this loop waiting
+ // on the first `agent_recent` while the dial sat on the old tile.
+ const rows: Array<{ id: string; past: Array<{ recap: string; text: string }> }> = []
for (const a of await this.host.listAgents()) {
- const past = await this.host.recentSummaries(a.id)
- // Oldest first, so the newest ends up on top of the tile's stack.
- for (const s of [...past].reverse()) {
- if (!s.recap && !s.text) continue
- await this.send({ t: 'summary', agentId: a.id, recap: s.recap, text: s.text, restore: true })
- }
+ rows.push({ id: a.id, past: await this.host.recentSummaries(a.id) })
}
+ return this.queued(async () => {
+ for (const row of rows) {
+ // Oldest first, so the newest ends up on top of the tile's stack.
+ for (const s of [...row.past].reverse()) {
+ if (!s.recap && !s.text) continue
+ await this.send({ t: 'summary', agentId: row.id, recap: s.recap, text: s.text, restore: true })
+ }
+ }
+ })
+ }
+
+ /**
+ * The same history, off the critical path.
+ *
+ * A machine switch owes the person two things and they are not equally urgent: the tile they clicked,
+ * NOW, and what every tile was last doing, eventually. `restore: true` says the second one is history —
+ * no beep, no notification — so nothing about it is worth a second of staring at the old tile.
+ */
+ private restoreInBackground(): void {
+ void this.pushRestores().catch((err) => this.log(`cable: restores failed (${(err as Error).message})`))
}
/** Attach: tell the dial everything, whether or not any of it looks unchanged from here. */
@@ -1247,7 +1264,7 @@ export class CableSession {
if (machineId === this.host.selectedMachine()) {
await this.send({ t: 'machine.selected', machineId })
await this.syncAgents(true)
- await this.pushRestores()
+ this.restoreInBackground()
return
}
const result = await this.host.selectMachine(machineId)
@@ -1262,8 +1279,10 @@ export class CableSession {
this.lastAgentsKey = ''
await this.send({ t: 'machine.selected', machineId })
await this.syncAgents(true)
- await this.pushRestores()
await this.syncMachines(true) // the ✓ moved, and the agent counts with it
+ // LAST, AND NOT AWAITED. This is what the caller is blocked on: `followApp` selects the machine and
+ // only then commands the focus, so anything awaited here is time the dial spends on the old tile.
+ this.restoreInBackground()
}
/** One row changed — liveness, a rename, a count. Cheaper than re-streaming the wheel. */
diff --git a/cli/src/device/deviceFleet.ts b/cli/src/device/deviceFleet.ts
index d03d1a274..ba1c01420 100644
--- a/cli/src/device/deviceFleet.ts
+++ b/cli/src/device/deviceFleet.ts
@@ -50,6 +50,8 @@ export class DeviceFleet implements MachineFleet {
private readonly recapCache = new Map<string, RecentTurn[]>()
/** Filled by the same `agent_recent` round trip the recaps come from — see recentSummaries. */
private readonly askCache = new Map<string, string[]>()
+ /** `agent_recent` round trips in flight, so two callers asking at once share one — see recentSummaries. */
+ private readonly recapInFlight = new Map<string, Promise<RecentTurn[]>>()
private readonly listeners = new Set<(e: FleetEvent) => void>()
/**
* How the last round trip to each machine went.
@@ -252,6 +254,21 @@ export class DeviceFleet implements MachineFleet {
const key = `${machineId}:${agentId}`
const cached = this.recapCache.get(key)
if (cached) return cached
+ // ONE round trip per agent, however many callers want it. Selecting a machine starts a prefetch AND a
+ // restore push, and both walk the same agents: without this the cold list is asked for twice, in two
+ // serial loops, and the second loop is the one the dial is waiting on.
+ const inflight = this.recapInFlight.get(key)
+ if (inflight) return inflight
+ const round = this.fetchRecent(machineId, agentId, key)
+ this.recapInFlight.set(key, round)
+ try {
+ return await round
+ } finally {
+ this.recapInFlight.delete(key)
+ }
+ }
+
+ private async fetchRecent(machineId: string, agentId: string, key: string): Promise<RecentTurn[]> {
try {
const res = await this.rpc(machineId, 'agent_recent', { agentId, n: 3 })