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
feat: report terminal_link_mode to the sink when stream transport modes change or are demoted
0xken1578496c892bca
diff --git a/cli/src/lib/remoteRelay.spec.ts b/cli/src/lib/remoteRelay.spec.tsnew file mode 100644index 000000000..3c95a1e15--- /dev/null+++ b/cli/src/lib/remoteRelay.spec.ts@@ -0,0 +1,109 @@+import { describe, expect, it, vi } from 'vitest'+import { RemoteRelayPool } from './remoteRelay.js'++/** Minimal fake of the private `Entry` shape `noteTerminalResponse`/`demoteP2p` operate on — exercised+ * directly (bypassing the class's `private` marker, the lightest way to unit-test this without a real+ * WebRTC negotiation or a live relay socket) since the reporting logic under test is pure bookkeeping+ * derived from `p2pStreams`/`p2pPendingOpens`, not anything that needs a real transport underneath it. */+function fakeEntry(overrides: Partial<Record<string, unknown>> = {}) {+ return {+ ws: { send: vi.fn() },+ crypto: { wrapOutgoing: (frame: unknown) => frame },+ sink: { sendFrame: vi.fn(() => true), sendBinary: vi.fn(() => true) },+ onClosed: null,+ lingerTimer: null,+ alive: true,+ heartbeatTimer: null,+ p2p: null,+ p2pPolicy: null,+ p2pPendingOpens: new Set<string>(),+ p2pStreams: new Set<string>(),+ ...overrides,+ }+}++describe('RemoteRelayPool reports terminal_link_mode to the local sink', () => {+ // Constructor args are never touched by the private methods under test — only entry-level state is.+ const pool = new RemoteRelayPool(+ { accessToken: async () => 'unused' } as never,+ 'ws://unused',+ { pub: new Uint8Array(), priv: new Uint8Array() } as never,+ { pin: () => {}, get: () => null } as never,+ ) as unknown as {+ noteTerminalResponse: (entry: ReturnType<typeof fakeEntry>, frame: Record<string, unknown>, transport: 'p2p' | 'relay') => void+ demoteP2p: (entry: ReturnType<typeof fakeEntry>, reason: string) => void+ }++ it('a terminal_ready confirmed on p2p reports mode:p2p, matching p2pStreams', () => {+ const entry = fakeEntry({ p2pPendingOpens: new Set(['req-1']) })+ pool.noteTerminalResponse(entry, { type: 'terminal_ready', payload: { requestId: 'req-1', streamId: 'stream-1' } }, 'p2p')++ expect(entry.p2pStreams.has('stream-1')).toBe(true)+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-1', mode: 'p2p' },+ })+ })++ it('a terminal_ready delivered over relay reports mode:relay', () => {+ const entry = fakeEntry({ p2pPendingOpens: new Set(['req-2']) })+ pool.noteTerminalResponse(entry, { type: 'terminal_ready', payload: { requestId: 'req-2', streamId: 'stream-2' } }, 'relay')++ expect(entry.p2pStreams.has('stream-2')).toBe(false)+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-2', mode: 'relay' },+ })+ })++ it('a stale/unmatched requestId never reports p2p, even if the transport param says p2p', () => {+ const entry = fakeEntry() // no pending open for 'req-3' — delete() will return false+ pool.noteTerminalResponse(entry, { type: 'terminal_ready', payload: { requestId: 'req-3', streamId: 'stream-3' } }, 'p2p')++ expect(entry.p2pStreams.has('stream-3')).toBe(false)+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-3', mode: 'relay' },+ })+ })++ it('a non-ready frame for a p2p stream that arrives over relay quietly demotes just that stream, and reports it', () => {+ const entry = fakeEntry({ p2pStreams: new Set(['stream-4']) })+ pool.noteTerminalResponse(entry, { type: 'terminal_alive', payload: { streamId: 'stream-4' } }, 'relay')++ expect(entry.p2pStreams.has('stream-4')).toBe(false)+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-4', mode: 'relay' },+ })+ })++ it('a frame for a stream not on p2p never spuriously reports anything', () => {+ const entry = fakeEntry()+ pool.noteTerminalResponse(entry, { type: 'terminal_output', payload: { streamId: 'stream-5' } }, 'relay')++ expect(entry.sink.sendFrame).not.toHaveBeenCalled()+ })++ it('demoteP2p reports mode:relay for every stream it drops', () => {+ const entry = fakeEntry({ p2pStreams: new Set(['stream-a', 'stream-b']) })+ pool.demoteP2p(entry, 'send_failed')++ expect(entry.p2pStreams.size).toBe(0)+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-a', mode: 'relay' },+ })+ expect(entry.sink.sendFrame).toHaveBeenCalledWith({+ type: 'terminal_link_mode',+ payload: { streamId: 'stream-b', mode: 'relay' },+ })+ })++ it('demoteP2p on a connection with no active p2p streams reports nothing', () => {+ const entry = fakeEntry()+ pool.demoteP2p(entry, 'send_failed')++ expect(entry.sink.sendFrame).not.toHaveBeenCalled()+ })+})diff --git a/cli/src/lib/remoteRelay.ts b/cli/src/lib/remoteRelay.tsindex 18e29bd52..81db9708f 100644--- a/cli/src/lib/remoteRelay.ts+++ b/cli/src/lib/remoteRelay.ts@@ -409,12 +409,22 @@ export class RemoteRelayPool {const requestId = typeof payload.requestId === 'string' ? payload.requestId : ''if (frame.type === 'terminal_ready' && requestId && streamId) {if (entry.p2pPendingOpens.delete(requestId) && transport === 'p2p') entry.p2pStreams.add(streamId)+ // Tell the local app (Desktop) which transport this stream just came up on — derived from+ // entry.p2pStreams' own post-update membership (the routing source of truth just above), not a+ // naive echo of `transport`, so a stale/duplicate terminal_ready can never report a mode that+ // doesn't match what's actually routing.+ entry.sink?.sendFrame({ type: 'terminal_link_mode', payload: { streamId, mode: entry.p2pStreams.has(streamId) ? 'p2p' : 'relay' } })} else if (frame.type === 'terminal_error' && requestId) {entry.p2pPendingOpens.delete(requestId)} else if (frame.type === 'terminal_closed' && streamId) {entry.p2pStreams.delete(streamId)}- if (transport === 'relay' && streamId) entry.p2pStreams.delete(streamId)+ // A non-terminal_ready frame for a stream still marked p2p but physically delivered over relay: a+ // quieter, single-stream demotion than demoteP2p() (which also tears down the whole p2p connection)+ // — still worth telling the local app about, since its badge would otherwise go stale.+ if (transport === 'relay' && streamId && entry.p2pStreams.delete(streamId)) {+ entry.sink?.sendFrame({ type: 'terminal_link_mode', payload: { streamId, mode: 'relay' } })+ }}private demoteP2p(entry: Entry, reason: string): void {@@ -426,6 +436,7 @@ export class RemoteRelayPool {for (const streamId of streamIds) {const resync = entry.crypto.wrapOutgoing({ type: 'terminal_resync', payload: { streamId } })try { entry.ws.send(JSON.stringify(resync)) } catch { /* relay close handles cleanup */ }+ entry.sink?.sendFrame({ type: 'terminal_link_mode', payload: { streamId, mode: 'relay' } })}if (streamIds.length > 0) this.reportP2pResult(entry, 'dropped', undefined, reason)void p2p?.stop(reason)