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.

autonomous-device: send sealed pair.revoke to the device on app-side unpair

leo4821d57922e8
diff --git a/cli/src/lib/autonomous-device/direct.spec.ts b/cli/src/lib/autonomous-device/direct.spec.ts
index b4c8f7e53..4b5740b96 100644
--- a/cli/src/lib/autonomous-device/direct.spec.ts
+++ b/cli/src/lib/autonomous-device/direct.spec.ts
@@ -34,8 +34,12 @@ describe('direct Autonomous device endpoint selection', () => {
await new Promise(resolve => setTimeout(resolve, 20))
expect(await f.direct.pair(f.row.id, 'CODE12')).toMatchObject({ state: 'paired', fingerprint: f.fp })
expect(f.attempts()).toBe(2)
+ const closed: number[] = []
+ for (const ws of f.wss.clients) ws.once('close', code => closed.push(code))
f.direct.revoked(f.fp)
expect(f.direct.connected()).toBe(0)
+ // Graceful close (not terminate) so a pair.revoke frame queued just before is flushed to the device.
+ await vi.waitFor(() => expect(closed).toEqual([1000]))
})
it('does not persist success if PAKE identity has not authenticated its session', async () => {
const f = await fixture()
diff --git a/cli/src/lib/autonomous-device/direct.ts b/cli/src/lib/autonomous-device/direct.ts
index ef82dc8c7..1a4412c42 100644
--- a/cli/src/lib/autonomous-device/direct.ts
+++ b/cli/src/lib/autonomous-device/direct.ts
@@ -81,7 +81,8 @@ export class AutonomousDeviceDirect {
revoked(fingerprint: string): void {
const ids = this.associations.filter(a => a.fingerprint === fingerprint).map(a => a.discoveryId)
this.associations = this.associations.filter(a => a.fingerprint !== fingerprint)
- for (const id of ids) { this.links.get(id)?.ws.terminate(); this.links.delete(id) }
+ // close(), not terminate(): the pair.revoke frame just queued by the relay must flush before the socket goes.
+ for (const id of ids) { this.links.get(id)?.ws.close(1000); this.links.delete(id) }
this.save()
}
private async reconnect(): Promise<void> {
diff --git a/cli/src/lib/autonomous-device/relay.spec.ts b/cli/src/lib/autonomous-device/relay.spec.ts
index cc5d0f84f..b9a35e01c 100644
--- a/cli/src/lib/autonomous-device/relay.spec.ts
+++ b/cli/src/lib/autonomous-device/relay.spec.ts
@@ -43,6 +43,17 @@ describe('Autonomous device existing E2EE relay seam', () => {
})
expect(f.remoteRevoke).toHaveBeenCalledWith('trusted-device')
})
+ it('app-side revoke sends exactly one sealed pair.revoke to the connected device, then drops it', async () => {
+ const f = fixture(); await f.request({ type: 'hello', proto: 1, requestId: randomUUID() })
+ f.send.mockClear()
+ f.relay.revoke('trusted-device')
+ const revokes = f.send.mock.calls.filter(([, frame]) => (frame as { payload: { __e2e: { type: string } } }).payload.__e2e.type === 'pair.revoke')
+ expect(revokes).toHaveLength(1)
+ expect(revokes[0][1]).toEqual({ type: 'autonomous_device_event', payload: { __e2e: { type: 'pair.revoke', machineId: 'machine' } } })
+ // Client state is gone: the next request needs a fresh application hello.
+ await f.request({ type: 'turn.send', requestId: randomUUID() })
+ expect(f.send.mock.calls.at(-1)?.[1]).toMatchObject({ payload: { __e2e: { error: { code: 'HELLO_REQUIRED' } } } })
+ })
it('does not revoke when the request is malformed', async () => {
const f = fixture(); await f.request({ type: 'hello', proto: 1, requestId: randomUUID() })
await f.request({ type: 'pair.revoke', requestId: randomUUID(), extra: true })
diff --git a/cli/src/lib/autonomous-device/relay.ts b/cli/src/lib/autonomous-device/relay.ts
index 65ebc741e..9e4045bbf 100644
--- a/cli/src/lib/autonomous-device/relay.ts
+++ b/cli/src/lib/autonomous-device/relay.ts
@@ -66,5 +66,14 @@ export class AutonomousDeviceRelay {
}
emit(event: AutonomousDeviceFrame): void { for (const [connId, client] of this.clients) this.sendEvent(connId, client.identity, event) }
drop(connId: string): void { this.clients.delete(connId) }
- revoke(identity: string): void { for (const [connId, client] of this.clients) if (client.identity === identity) this.clients.delete(connId); this.service.revoke(identity) }
+ /** App-side revoke: tell the device while its authenticated session still exists (the E2eeManager drops
+ * the session right after), so it clears its own pin instead of showing "paired / disconnected" forever.
+ * Best-effort: a device that is offline learns it on reconnect, when its e2e_hello gets e2e_denied. */
+ revoke(identity: string): void {
+ for (const [connId, client] of this.clients) if (client.identity === identity) {
+ try { this.sendTo(connId, identity, 'autonomous_device_event', { type: 'pair.revoke', machineId: this.machineId }) } catch { /* Local removal must proceed regardless. */ }
+ this.clients.delete(connId)
+ }
+ this.service.revoke(identity)
+ }
}
diff --git a/cli/src/lib/e2ee/manager.test.ts b/cli/src/lib/e2ee/manager.test.ts
index 78b1bbfa2..5e78cb986 100644
--- a/cli/src/lib/e2ee/manager.test.ts
+++ b/cli/src/lib/e2ee/manager.test.ts
@@ -153,9 +153,9 @@ class PwPeer {
}
}
-function machine() {
+function machine(extra: Partial<ConstructorParameters<typeof makeManager>[0]> = {}) {
const sent: Array<{ connId: string; frame: Frame }> = []
- const mgr = new makeManager({ machineId: AGENT, sendTo: (connId, frame) => sent.push({ connId, frame }), isConnected: () => true })
+ const mgr = new makeManager({ machineId: AGENT, sendTo: (connId, frame) => sent.push({ connId, frame }), isConnected: () => true, ...extra })
const takeLast = (type: string): Frame => {
for (let i = sent.length - 1; i >= 0; i--) if (sent[i].frame.type === type) return sent[i].frame
throw new Error(`no ${type} sent`)
@@ -369,6 +369,24 @@ describe('E2eeManager revoke', () => {
expect(C.unwrapPayload(oldGroupKey, newEnv, 'text_delta', 's')).toBeNull()
})
+ it('a reconnect hello from a revoked identity is answered with e2e_denied (unpaired)', async () => {
+ const h = machine()
+ const web = await fullPair(h, 'cr')
+ expect(h.mgr.revoke(C.fingerprint(web.identity.pub)).ok).toBe(true)
+ h.mgr.handleFrame('cr2', web.hello())
+ expect((h.lastFor('cr2', 'e2e_denied')!.payload as Record<string, unknown>).reason).toBe('unpaired')
+ expect(h.mgr.hasSession('cr2')).toBe(false)
+ })
+
+ it('revoke-all runs onIdentityRevoked while the session is still live, so a device frame can be sealed', async () => {
+ const live: boolean[] = []
+ const h = machine({ onIdentityRevoked: () => live.push(h.mgr.hasSession('ra')) })
+ await fullPair(h, 'ra')
+ h.mgr.revokeAll()
+ expect(live).toEqual([true])
+ expect(h.mgr.hasSession('ra')).toBe(false)
+ })
+
it('revoke re-keys the REMAINING browsers so they keep decrypting', async () => {
const h = machine()
const web1 = await fullPair(h, 'a1')
diff --git a/cli/src/lib/e2ee/manager.ts b/cli/src/lib/e2ee/manager.ts
index 0f03f91aa..270ef585b 100644
--- a/cli/src/lib/e2ee/manager.ts
+++ b/cli/src/lib/e2ee/manager.ts
@@ -234,8 +234,9 @@ export class E2eeManager {
/** Revoke every paired browser. Signals all online sessions to re-pair + rotates the group key. */
revokeAll(): { count: number } {
const count = this.store.count()
- for (const s of [...this.sessions.entries()]) { this.deny(s[0]); this.sessions.delete(s[0]) }
+ // Device cleanup first: it needs the live session to seal a pair.revoke frame before deny drops it.
for (const paired of this.store.list()) { try { this.deps.onIdentityRevoked?.(paired.identityPub) } catch { /* Continue revoking every stored identity. */ } }
+ for (const s of [...this.sessions.entries()]) { this.deny(s[0]); this.sessions.delete(s[0]) }
this.store.clear()
this.rotateGroupKey()
return { count }
diff --git a/docs/autonomous-device-integration.md b/docs/autonomous-device-integration.md
index 8a048757e..94914a6d2 100644
--- a/docs/autonomous-device-integration.md
+++ b/docs/autonomous-device-integration.md
@@ -77,6 +77,13 @@ The CLI then removes that exact device identity and its reconnect metadata. A so
this request remains `offline`, rather than being treated as a revoke, so transient LAN failures do
not unpair the device.
+Revoke is bidirectional. When the app removes the device (`harness unpair`, `harness unpair --all`,
+`harness autonomous-device revoke`, or the dashboard) while the device's direct session is open, the
+CLI seals `{type:"pair.revoke",machineId:<this computer's machineId>}` as an `autonomous_device_event`
+over that same E2EE session, then closes the socket gracefully and deletes local trust. It is
+best-effort: a send failure never blocks local removal. A device that is offline at that moment
+learns it on reconnect, when its pinned `e2e_hello` is answered with `e2e_denied` (`unpaired`).
+
## Existing encrypted wire, unchanged
Client identity and session use `cli/src/lib/e2ee/core.ts` and `manager.ts` exactly:
diff --git a/docs/vi/autonomous-device-integration_vi.md b/docs/vi/autonomous-device-integration_vi.md
index f3e4715ba..ec1f7c894 100644
--- a/docs/vi/autonomous-device-integration_vi.md
+++ b/docs/vi/autonomous-device-integration_vi.md
@@ -40,6 +40,13 @@ Khi device tự revoke trust cục bộ, nó gửi application request đã xác
identity đó cùng metadata reconnect. Socket chỉ đóng mà không có request này vẫn là `offline`, không
được hiểu là revoke, để lỗi mạng thoáng qua không unpair device.
+Revoke là hai chiều. Khi app gỡ device (`harness unpair`, `harness unpair --all`,
+`harness autonomous-device revoke`, hoặc dashboard) trong lúc session direct của device đang mở, CLI
+seal `{type:"pair.revoke",machineId:<machineId của máy này>}` dưới dạng `autonomous_device_event` trên
+chính session E2EE đó, rồi đóng socket nhẹ nhàng và xóa trust local. Best-effort: gửi lỗi không chặn
+việc gỡ local. Device đang offline lúc đó sẽ biết khi reconnect: `e2e_hello` với pin cũ bị trả
+`e2e_denied` (`unpaired`).
+
Wire dùng nguyên e2e_* gốc role device, CI b:device. App outer autonomous_device_request/result/event
vẫn pairwise encrypted với empty dbSessionId AAD; helper relay.ts chỉ là adapter ứng dụng dùng chung
crypto, không kết nối backend. Request agents.list/status/recap/turn.send/turn.stop/question.answer/