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
harnessd: replies to the relay go to the client that asked, sealed (#792)
Dee22d37d436dc4
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex b4eff8e37..87402223e 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -32,7 +32,7 @@ import { fakeGridAnswers, installFakeGrid, type FakeGrid } from './lib/__fixtureimport { clearGridMcpUrlCache } from './lib/gridMcpUrl.js'import { LocalModels } from './lib/localModels.js'import type { GridAttachResult } from './lib/gridAttach.js'-import { STRICT_DOWN_TYPES, encryptDownFrame, encryptRpcResult } from './lib/e2ee/applicationFrames.js'+import { STRICT_DOWN_TYPES, encryptDownFrame, encryptDownFrameFor, encryptRpcResult } from './lib/e2ee/applicationFrames.js'import { CloseAgentService } from './lib/closeAgentService.js'import { env } from './config/env.js'@@ -561,6 +561,13 @@ function serveModels(socket: BackendSocket, over: Parameters<typeof fakeCore>[0]return host}+/** The session a requester holds whenever its sealed request was opened. Replies of a sealed type go back+ * sealed to it; this stub seals them transparently, so a test reads the reply as the client does, opened. */+function withSession(socket: BackendSocket, connId: string): void {+ vi.spyOn(socket.e2ee, 'hasSession').mockImplementation((id) => id === connId)+ vi.spyOn(socket.e2ee, 'wrapRpcReply').mockImplementation((_id, type, requestId, payload) => ({ type, payload: { requestId, ...payload } }))+}+describe('BackendSocket outbound queue', () => {afterEach(() => {wsMock.instances.length = 0@@ -856,7 +863,7 @@ describe('BackendSocket outbound queue', () => {// as a reply that carries nothing is.role.mockReturnValue('web')ask('nobody_answers', { requestId: 'n-1' })- await vi.waitFor(() => expect(parseSent(ws)).toContainEqual({ t: 'up', frame: { type: 'nobody_answers_result', payload: { requestId: 'n-1', error: 'UNSUPPORTED' } } }))+ await vi.waitFor(() => expect(parseSent(ws)).toContainEqual({ t: 'up', targetConnId: 'web-1', frame: { type: 'nobody_answers_result', payload: { requestId: 'n-1', error: 'UNSUPPORTED' } } }))expect(routed.at(-1)?.type).toBe('nobody_answers')// A process on this machine asks as itself, and as the owner.@@ -940,15 +947,97 @@ describe('BackendSocket outbound queue', () => {expect(JSON.stringify((socket as unknown as { queue: unknown[] }).queue)).not.toContain('gone-')ws.open()expect(JSON.stringify(parseSent(ws))).not.toContain('gone-')- // A request from the relay is still answered on the relay, as before.+ // A request from the relay is still answered on the relay, sealed to the session that asked.+ vi.spyOn(socket.e2ee, 'hasSession').mockImplementation((connId) => connId === 'web-1')+ vi.spyOn(socket.e2ee, 'wrapRpcReply').mockImplementation((_connId, type) => ({ type, payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } } }))vi.spyOn(socket.e2ee, 'unwrapDown').mockReturnValueOnce({ type: 'terminal_info', payload: { requestId: 'relay-info', agentId: 'a1' } })ws.message({ t: 'down', connId: 'web-1', frame: { type: 'terminal_info', payload: { __e2e: { v: 1, k: 's', n: 1, ct: 'ciphertext' } } } })await vi.waitFor(() => expect(answers.has('relay-info')).toBe(true))answers.get('relay-info')!({ error: 'AGENT_NOT_FOUND' })- expect(parseSent(ws)).toContainEqual({ t: 'up', frame: { type: 'terminal_info_result', payload: { requestId: 'relay-info', error: 'AGENT_NOT_FOUND' } } })+ expect(parseSent(ws)).toContainEqual({ t: 'up', targetConnId: 'web-1', frame: { type: 'terminal_info_result', payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } } } })await socket.stop()})+ it('answers a request from the relay to the requester alone: no other web client and no window gets a copy', async () => {+ // Each app takes a reply by its own request id; a broadcast handed every web client of the machine+ // and every window on it an answer only one of them had asked for.+ const socket = new BackendSocket('token')+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ const window: Array<Record<string, unknown>> = []+ socket.registerLocalClient('local:window', { sendFrame: (frame) => { window.push(frame as Record<string, unknown>); return true }, sendBinary: () => true })+ // A paired client's request, sealed, of a type whose answer carries nothing: answered in the clear.+ vi.spyOn(socket.e2ee, 'unwrapDown').mockReturnValueOnce({ type: 'nobody_answers', payload: { requestId: 'r-1' } })+ ws.message({ t: 'down', connId: 'web-1', frame: { type: 'nobody_answers', payload: { __e2e: { v: 1, k: 's', n: 1, ct: 'ciphertext' } } } })+ const answers = () => parseSent(ws).filter((item) => (item.frame as { type?: string })?.type === 'nobody_answers_result')+ await vi.waitFor(() => expect(answers()).toHaveLength(1))+ expect(answers()).toEqual([{ t: 'up', targetConnId: 'web-1', frame: { type: 'nobody_answers_result', payload: { requestId: 'r-1', error: 'UNSUPPORTED' } } }])+ expect(window.filter((frame) => frame.type === 'nobody_answers_result')).toEqual([])+ // The backend's own request has no connection to be answered on: it still hears its answer on the bus.+ ws.message({ t: 'down', connId: '', frame: { type: 'voice_route', payload: { requestId: 'b-1', transcript: 'route this' } } })+ await vi.waitFor(() => expect(parseSent(ws)).toContainEqual({ t: 'up', frame: { type: 'voice_route_result', payload: { requestId: 'b-1', error: 'E2EE_REQUIRED' } } }))+ await socket.stop()+ })++ it('seals what a pane runs and where a spoken task went to a requester that holds a session, and to it alone', async () => {+ // A pane's folder, pid and tty, and the router's restatement of a spoken task, used to go to every+ // web client in the clear. Every client that holds a session opens a sealed payload of any type.+ const socket = new BackendSocket('token')+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ vi.spyOn(socket.e2ee, 'hasSession').mockImplementation((connId) => connId === 'web-1')+ const wrap = vi.spyOn(socket.e2ee, 'wrapRpcReply').mockImplementation((_connId, type) => ({ type, payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } } }))+ const unwrap = vi.spyOn(socket.e2ee, 'unwrapDown')+ const ask = (connId: string, type: string, payload: Record<string, unknown>) => {+ unwrap.mockReturnValueOnce({ type, payload })+ ws.message({ t: 'down', connId, frame: { type, payload: { __e2e: { v: 1, k: 's', n: 1, ct: 'ciphertext' } } } })+ }+ const answers = () => parseSent(ws).filter((item) => ['terminal_info_result', 'voice_route_result'].includes(String((item.frame as { type?: unknown })?.type)))+ ask('web-1', 'terminal_info', { requestId: 'info-1', agentId: 'no-such-agent' })+ ask('web-1', 'voice_route', { requestId: 'route-1', transcript: '' })+ await vi.waitFor(() => expect(answers()).toHaveLength(2))+ expect(wrap).toHaveBeenCalledWith('web-1', 'terminal_info_result', 'info-1', { error: 'AGENT_NOT_FOUND' })+ expect(wrap).toHaveBeenCalledWith('web-1', 'voice_route_result', 'route-1', { error: 'MISSING_TRANSCRIPT' })+ const sealed = { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } }+ expect(answers()).toEqual([+ { t: 'up', targetConnId: 'web-1', frame: { type: 'terminal_info_result', payload: sealed } },+ { t: 'up', targetConnId: 'web-1', frame: { type: 'voice_route_result', payload: sealed } },+ ])+ // A requester whose session went away meanwhile gets the bare refusal a refused request gets, never+ // the answer in the clear.+ ask('web-2', 'terminal_info', { requestId: 'info-2', agentId: 'no-such-agent' })+ await vi.waitFor(() => expect(answers()).toHaveLength(3))+ expect(answers()[2]).toEqual({ t: 'up', targetConnId: 'web-2', frame: { type: 'terminal_info_result', payload: { requestId: 'info-2', error: 'E2EE_REQUIRED' } } })+ // Only the replies: the requests keep the rule they had, since an older daemon reads a sealed one as empty.+ for (const type of ['terminal_info', 'voice_route']) {+ expect(encryptDownFrame(type)).toBe(false)+ expect(encryptDownFrameFor(type, { strictDown: true })).toBe(false)+ expect(encryptRpcResult(`${type}_result`)).toBe(true)+ }+ await socket.stop()+ })++ it.each(['dsh_list', 'dsh_install', 'dsh_update', 'dsh_remove', 'engines_probe', 'grid_models_list', 'claude_login_status', 'agent_retarget', 'remote_terminal_handoff'])(+ 'seals the %s answer to a requester that holds a session, and to it alone', async (type) => {+ // What a machine tells the client that asked about it went to every web client in the clear.+ const socket = new BackendSocket('token')+ socket.connect()+ const ws = wsMock.instances[0]+ ws.open()+ vi.spyOn(socket.e2ee, 'hasSession').mockReturnValue(true)+ const sealed = { __e2e: { v: 1, k: 'p', n: 1, ct: 'ciphertext' } }+ const wrap = vi.spyOn(socket.e2ee, 'wrapRpcReply').mockImplementation((_connId, resultType) => ({ type: resultType, payload: sealed }))+ ;(socket as unknown as { emitReply(connId: string, type: string, requestId: unknown, payload: Record<string, unknown>): void })+ .emitReply('web-1', type, 'r-1', { account: 'this-computer' })+ expect(wrap).toHaveBeenCalledWith('web-1', `${type}_result`, 'r-1', { account: 'this-computer' })+ expect(parseSent(ws).filter((item) => (item.frame as { type?: unknown })?.type === `${type}_result`))+ .toEqual([{ t: 'up', targetConnId: 'web-1', frame: { type: `${type}_result`, payload: sealed } }])+ expect(JSON.stringify(parseSent(ws))).not.toContain('this-computer')+ await socket.stop()+ })+it('hands theme_set to the host-theme sink and acknowledges it to the requester', async () => {const socket = new BackendSocket('token')const received: unknown[] = []@@ -2643,6 +2732,7 @@ describe('agent_retarget clearGrid', () => {socket.connect()const ws = wsMock.instances[0]ws.open()+ withSession(socket, 'web-1')ws.message(sealedDown(socket, 'web-1', 'agent_retarget', payload))await vi.waitFor(() => expect(ws.sent.length).toBeGreaterThan(0))const reply = parseSent(ws)@@ -2699,6 +2789,7 @@ describe('agent_retarget onto a Local model resolves web tools', () => {ws.open()// The grid name is the backend's, pushed on connect; the daemon holds it in memory only.ws.message({ t: 'down', connId: '', frame: { type: 'machine_meta', payload: { name: 'mac', gridName: GRID_NAME } } })+ withSession(socket, 'web-1')ws.message(sealedDown(socket, 'web-1', 'agent_retarget', { requestId: 'r', agentId: 'a1', gridModel: model }))await vi.waitFor(() => expect(parseSent(ws).some((item) => (item.frame as { type?: string } | undefined)?.type === 'agent_retarget_result')).toBe(true), { timeout: 10_000 })const reply = parseSent(ws)@@ -2866,6 +2957,7 @@ describe('grid_models_list says whether this machine has a grid CLI', () => {const ws = wsMock.instances[0]ws.open()ws.message({ t: 'down', connId: '', frame: { type: 'machine_meta', payload: { name: 'mac', gridName: GRID_NAME } } })+ withSession(socket, 'web-1')ws.message(sealedDown(socket, 'web-1', 'grid_models_list', { requestId: 'r' }))await vi.waitFor(() => expect(parseSent(ws).some((item) => (item.frame as { type?: string } | undefined)?.type === 'grid_models_list_result')).toBe(true), { timeout: 10_000 })const reply = parseSent(ws)diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 1aae22257..1c8e64a46 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -1515,17 +1515,15 @@ export class BackendSocket {const wrapped = this.e2ee.wrapRpcReply(connId, resultType, requestId, replyPayload)if (wrapped) { this.sendTo(connId, wrapped); return }}- // Enforcement ("no E2EE ⇒ no adapter data"): content-bearing RPC replies must never leave the- // adapter plaintext. A real client gets a targeted error; the legacy backend nodeRequest awaiter- // (`connId === ''`) gets a broadcast error with the same requestId so it fails closed without data.- //- if (encryptRpcResult(resultType)) {- const errorFrame = { type: resultType, payload: { requestId, error: 'E2EE_REQUIRED' } }- if (connId) this.sendTo(connId, errorFrame)- else this.send(errorFrame)- return- }- this.send({ type: resultType, payload: { requestId, ...payload } })+ // Enforcement ("no E2EE ⇒ no adapter data"): a content-bearing reply that could not be sealed is a+ // bare E2EE_REQUIRED, never its content in the clear. Either way it goes to the requester alone:+ // through `send()` a reply went to every web client of this machine and every window on it, though+ // each app takes a reply by its own request id and ignores the rest (ws_conn.dart in the desktop and+ // phone apps, hn's daemon.rs, the CLI's relay pool). The backend's own nodeRequest (`connId === ''`)+ // has no connection to be answered on: it hears the reply on the bus, and fails closed on the error.+ const reply = { type: resultType, payload: encryptRpcResult(resultType) ? { requestId, error: 'E2EE_REQUIRED' } : { requestId, ...payload } }+ if (connId) this.sendTo(connId, reply)+ else this.send(reply)}private async dispatchDown(frame: Frame, connId: string, transport: DownTransport = 'relay'): Promise<void> {diff --git a/cli/src/lib/e2ee/applicationFrames.ts b/cli/src/lib/e2ee/applicationFrames.tsindex bdcc21097..f194ac376 100644--- a/cli/src/lib/e2ee/applicationFrames.ts+++ b/cli/src/lib/e2ee/applicationFrames.ts@@ -73,6 +73,29 @@ export const encryptDownFrameFor = (type: string, peer: { strictDown: boolean })* machine relaying for its app — rather than being broadcast to every window and web client of this* machine. Here and not in core.ts's pinned set: the reply is the same frame, only its route changes. */const QUESTION_RESULT = 'question_response_result'+/**+ * Replies a daemon used to send in the clear, and to every web client: sealed to the requester now.+ * Only the replies. Their requests keep the rule they had (sealed only where STRICT_DOWN_TYPES says, to+ * a daemon whose welcome says it opens them), because an older daemon reads a sealed request as empty.+ * No client tells the daemon it can open these, and none has to: every client that holds a session+ * opens a sealed payload of any type with its session key, and has since it was written (relayClient.ts+ * and relay_session_crypto.dart in the desktop and phone apps, `unwrapIncoming`). A requester with no+ * session gets the bare E2EE_REQUIRED a refused request already gets, and a window on this computer the+ * plain reply it always had.+ */+const SEALED_REPLIES = new Set([+ // What a pane runs and where: its folder, pid and tty.+ 'terminal_info_result',+ // Which agent a spoken task went to, and the router's reason, which restates the task.+ 'voice_route_result',+ // The rest of what a machine tells the client that asked about it: its Store harnesses, its engines+ // and their versions, its grids and models, the login an engine uses (with the computer's name), a+ // move onto a model, and a terminal handed between machines (refused over the relay).+ 'dsh_list_result', 'dsh_install_result', 'dsh_update_result', 'dsh_remove_result',+ 'engines_probe_result', 'grid_models_list_result', 'claude_login_status_result', 'agent_retarget_result',+ 'remote_terminal_handoff_result',+])export const encryptRpcResult = (type: string): boolean =>ENCRYPTED_RPC_RESULT_TYPES.has(type) || MACHINE_RESULTS.has(type) || FLEET_RESULTS.has(type) || SHARE_RESULT_TYPES.has(type)|| PAIR_RESULTS.has(type) || type === LOCAL_PAIR_RESULT || type === QUESTION_RESULT || type === PLATE_RESULT || TEAM_RESULT_TYPES.has(type)+ || SEALED_REPLIES.has(type)diff --git a/cli/src/lib/e2ee/relayLink.spec.ts b/cli/src/lib/e2ee/relayLink.spec.tsindex d1682f7f0..629a94546 100644--- a/cli/src/lib/e2ee/relayLink.spec.ts+++ b/cli/src/lib/e2ee/relayLink.spec.ts@@ -365,6 +365,31 @@ describe('remote-password link + relay session crypto (interop with the real E2eexpect(manager.untrustPeer(C.b64e(other.pub))).toBe(false)})+ it('replies the daemon now seals to the requester open on the relay client as it has always shipped', async () => {+ // The relay client opens a sealed payload of any type with its session key, and has since it was+ // written: a reply type the daemon starts sealing reaches every CLI relaying for a desktop or hn.+ const frames: Frame[] = []+ const manager = new E2eeManagerCtor({ machineId: MACHINE_ID, sendTo: (_c, f) => frames.push(f), isConnected: () => true })+ const client = C.newIdentity()+ manager.trustPeer({ pub: C.b64e(client.pub), machineId: 'a1b2c3d4e5f60718293a4b5c6d7e8f90', kind: 'machine', label: 'relay' })+ const { peekIdentityPub } = await import('./store.js')+ const crypto = new RelaySessionCrypto({ machineId: MACHINE_ID, selfIdentity: client, peerPub: C.b64d(peekIdentityPub()!) })+ expect(manager.handleFrame('session-conn', crypto.helloFrame())).toBe(true)+ expect(crypto.handleWelcome(frames.at(-1)!.payload as Record<string, unknown>)).toBe(true)+ const replies: Array<[string, string, Record<string, unknown>]> = [+ ['terminal_info_result', '/private/work/app', { requestId: 'info-1', command: 'node', path: '/private/work/app', pid: 4242, tty: '/dev/ttys001' }],+ ['voice_route_result', 'the login page', { requestId: 'route-1', agentId: 'a1', agentName: 'api', reason: 'asked to fix the login page', confidence: 0.9, needNewAgent: false }],+ ]+ for (const type of ['dsh_list', 'dsh_install', 'dsh_update', 'dsh_remove', 'engines_probe', 'grid_models_list', 'claude_login_status', 'agent_retarget', 'remote_terminal_handoff']) {+ replies.push([`${type}_result`, 'what-the-machine-said', { requestId: `${type}-1`, detail: 'what-the-machine-said' }])+ }+ for (const [type, secret, answer] of replies) {+ const sealed = manager.wrapRpcReply('session-conn', type, answer.requestId, answer)!+ expect(JSON.stringify(sealed)).not.toContain(secret)+ expect(crypto.unwrapIncoming(sealed)).toEqual({ type, payload: answer })+ }+ })+it('NO_REMOTE_PASSWORD when the target machine never set one', async () => {const { manager, wsBase, close } = fakeMachine()try {