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.

perf: benchmark isolated routes with ICE and wire verification

dee15a2c87423fd
diff --git a/cli/scripts/benchmark-route.test.ts b/cli/scripts/benchmark-route.test.ts
new file mode 100644
index 000000000..1c773dfb2
--- /dev/null
+++ b/cli/scripts/benchmark-route.test.ts
@@ -0,0 +1,40 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { candidateSummary, verifiedRoute, verifyExchange, type RouteSnapshot } from './benchmark-route.js'
+
+test('candidate evidence keeps type/protocol but removes addresses and ports', () => {
+ assert.deepEqual(candidateSummary('candidate:1 1 UDP 123 192.0.2.1 12345 typ srflx raddr 10.0.0.1 rport 23456'), { type: 'srflx', protocol: 'udp' })
+ assert.equal(candidateSummary('not a candidate'), null)
+ assert.equal(candidateSummary('candidate:1 1 UDP 123 192.0.2.1 12345'), null)
+})
+
+const direct = { local: { type: 'host', protocol: 'udp' }, remote: { type: 'srflx', protocol: 'udp' } }
+test('relay on either candidate means TURN; missing proof and migration are unknown', () => {
+ assert.equal(verifiedRoute(true, true, false, direct), 'p2p')
+ assert.equal(verifiedRoute(true, true, false, { ...direct, remote: { type: 'relay', protocol: 'udp' } }), 'turn')
+ assert.equal(verifiedRoute(true, true, false, { ...direct, local: { type: 'relay', protocol: 'udp' } }), 'turn')
+ assert.equal(verifiedRoute(true, true, false, null), 'unknown')
+ assert.equal(verifiedRoute(true, false, false, direct), 'unknown')
+ assert.equal(verifiedRoute(false, true, true, direct), 'unknown')
+ assert.equal(verifiedRoute(false, false, false, null), 'relay')
+})
+
+const before: RouteSnapshot = { route: 'p2p', ready: true, migrating: false, pair: direct,
+ sent: { p2p: 4, relay: 2 }, received: { p2p: 10, relay: 5 } }
+const after: RouteSnapshot = { ...before, sent: { p2p: 5, relay: 2 }, received: { p2p: 11, relay: 5 } }
+test('accept one direct input and its response, reject fallback, missing send, or pair change', () => {
+ assert.equal(verifyExchange('p2p', before, after), true)
+ assert.equal(verifyExchange('turn', before, after), false)
+ assert.equal(verifyExchange('p2p', before, { ...after, sent: { p2p: 4, relay: 3 } }), false)
+ assert.equal(verifyExchange('p2p', before, { ...after, received: { p2p: 11, relay: 6 } }), false)
+ assert.equal(verifyExchange('p2p', before, { ...after, received: before.received }), false)
+ assert.equal(verifyExchange('p2p', before, { ...after, pair: null }), false)
+})
+
+test('relay requires both wire directions and rejects a mixed route', () => {
+ const relayBefore: RouteSnapshot = { ...before, route: 'relay', pair: null, ready: false }
+ const relayAfter: RouteSnapshot = { ...relayBefore, sent: { p2p: 4, relay: 3 }, received: { p2p: 10, relay: 6 } }
+ assert.equal(verifyExchange('relay', relayBefore, relayAfter), true)
+ assert.equal(verifyExchange('relay', relayBefore, { ...relayAfter, sent: { p2p: 5, relay: 3 } }), false)
+ assert.equal(verifyExchange('relay', relayBefore, { ...relayAfter, route: 'p2p' }), false)
+})
diff --git a/cli/scripts/benchmark-route.ts b/cli/scripts/benchmark-route.ts
new file mode 100644
index 000000000..669b1d951
--- /dev/null
+++ b/cli/scripts/benchmark-route.ts
@@ -0,0 +1,224 @@
+// Opt-in benchmark adapter. Uses production transport in its own process/pool;
+// never changes the installed daemon, saved trust, authentication, or routing.
+import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
+import { homedir, tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { WebSocket, WebSocketServer } from 'ws'
+import type { AuthSessionManager } from '../src/lib/authSession.js'
+import type { MachinePeer, MachinePeerStore } from '../src/lib/e2ee/machinePeers.js'
+import type { RemoteRelayPool, RelaySession } from '../src/lib/remoteRelay.js'
+import { decodeTerminalLocal } from '../src/lib/terminalBinary.js'
+
+export type BenchmarkRoute = 'p2p' | 'turn' | 'relay'
+type Counts = { p2p: number; relay: number }
+type Candidate = { type: string; protocol: string }
+export type RouteSnapshot = {
+ route: BenchmarkRoute | 'unknown'; ready: boolean; migrating: boolean;
+ pair: { local: Candidate; remote: Candidate } | null;
+ sent: Counts; received: Counts;
+}
+
+// Never retain SDP, addresses, ports, keys, or ICE credentials in an artifact.
+export function candidateSummary(sdp: string | undefined): Candidate | null {
+ const parts = sdp?.trim().split(/\s+/) ?? []
+ const type = parts[parts.indexOf('typ') + 1]
+ const protocol = parts[2]?.toLowerCase()
+ if (!['host', 'srflx', 'prflx', 'relay'].includes(type) || !['udp', 'tcp'].includes(protocol)) return null
+ return { type, protocol }
+}
+
+export function verifiedRoute(onDataChannel: boolean, ready: boolean, migrating: boolean,
+ pair: RouteSnapshot['pair']): RouteSnapshot['route'] {
+ if (migrating) return 'unknown'
+ if (!onDataChannel) return 'relay'
+ if (!ready || !pair) return 'unknown'
+ return pair.local.type === 'relay' || pair.remote.type === 'relay' ? 'turn' : 'p2p'
+}
+
+export function verifyExchange(route: BenchmarkRoute, before: RouteSnapshot, after: RouteSnapshot): boolean {
+ const wire = route === 'relay' ? 'relay' : 'p2p'
+ const other = wire === 'relay' ? 'p2p' : 'relay'
+ return before.route === route && after.route === route
+ && JSON.stringify(before.pair) === JSON.stringify(after.pair)
+ && after.sent[wire] - before.sent[wire] === 1
+ && after.received[wire] > before.received[wire]
+ && after.sent[other] === before.sent[other]
+ && after.received[other] === before.received[other]
+}
+
+// These deliberately read the pinned werift/production implementation, rather
+// than adding a product diagnostics API just for a benchmark. Missing internals
+// fail closed. Revalidate against changes to RemoteRelayPool/werift.
+type InspectedEntry = {
+ ws: WebSocket;
+ p2p: any;
+ p2pStreams: Set<string>;
+ streams: Set<string>;
+ p2pMigrating: Map<string, number>;
+ p2pPolicy: { turn?: { urls: string[] } } | null;
+}
+type InspectedPool = {
+ entries: Map<string, InspectedEntry>;
+ handleP2pData: (...args: any[]) => void;
+ startP2p: (machineId: string, entry: InspectedEntry) => void;
+}
+
+export async function createBenchmarkRoute(machineId: string, route: BenchmarkRoute) {
+ // env.ts has legacy adoption at import time. Refuse a non-established install
+ // and use an explicit scratch directory to bypass the remaining migrations.
+ if (!existsSync(join(homedir(), '.harness', 'computer-id'))) throw new Error('Established Harness computer identity required')
+ const stateDir = process.env.ADAPTER_DATA_DIR || join(homedir(), '.harness', 'cli', 'data')
+ const rawIdentity = JSON.parse(readFileSync(join(stateDir, 'e2e', 'identity.json'), 'utf8'))
+ const identity = { priv: Buffer.from(rawIdentity.priv, 'base64'), pub: Buffer.from(rawIdentity.pub, 'base64') }
+ if (identity.priv.length !== 32 || identity.pub.length !== 32) throw new Error('Invalid existing identity')
+ const savedPeers = JSON.parse(readFileSync(join(stateDir, 'e2e', 'machinePeers.json'), 'utf8')) as MachinePeer[]
+ const pin = savedPeers.find((peer) => peer.machineId === machineId)
+ if (!pin || Buffer.from(pin.pub, 'base64').length !== 32) throw new Error('Requested machine is not linked')
+ const peers = new Map([[machineId, pin]])
+ const scratch = mkdtempSync(join(tmpdir(), 'harness-route-'))
+ process.env.ADAPTER_DATA_DIR = scratch
+ process.env.TERMINAL_P2P_FORCE_RELAY = route === 'turn' ? 'true' : 'false'
+ let pool: RemoteRelayPool | undefined
+ let server: WebSocketServer | undefined
+ try {
+ const { readAuthSession } = await import('../src/lib/authSession.js')
+ const { RemoteRelayPool: Pool } = await import('../src/lib/remoteRelay.js')
+ const { env } = await import('../src/config/env.js')
+ const authSession = readAuthSession()
+ if (!authSession) throw new Error('Existing Harness login required')
+ const autonomousEnv = authSession.autonomousEnv
+ // Normal credential consumption, read-only. Let the running daemon own
+ // refreshes; this probe cannot rotate a token or clear a user's login.
+ const auth = { accessToken: async () => {
+ const current = readAuthSession()
+ if (!current || current.autonomousEnv !== autonomousEnv) throw new Error('Harness login changed')
+ if (current.expiresAt && current.expiresAt <= Date.now()) throw new Error('Access token expired; retry after daemon refresh')
+ return current.accessToken
+ } } as AuthSessionManager
+ const trust = { get: (id: string) => peers.get(id) ?? null, unlink: (id: string) => peers.delete(id) } as MachinePeerStore
+ pool = new Pool(auth, env.BACKEND_WS_URL.replace(/\/$/, ''), identity, trust, { p2p: route !== 'relay' })
+ const inspected = pool as unknown as InspectedPool
+ if (route === 'p2p') {
+ // Restrict only this benchmark connection's offer policy. Without this,
+ // ICE can nominate a working TURN pair before checking a direct pair.
+ // The responder obtains its TURN credentials from the same offer.
+ const startP2p = inspected.startP2p.bind(pool)
+ inspected.startP2p = (id, entry) => {
+ if (entry.p2pPolicy) {
+ const { turn: _turn, ...directPolicy } = entry.p2pPolicy
+ entry.p2pPolicy = directPolicy
+ }
+ startP2p(id, entry)
+ }
+ }
+ const sent: Counts = { p2p: 0, relay: 0 }
+ const received: Counts = { p2p: 0, relay: 0 }
+ let receivingP2p = false
+ const handleP2pData = inspected.handleP2pData.bind(pool)
+ inspected.handleP2pData = (...args) => {
+ receivingP2p = true
+ try { handleP2pData(...args) } finally { receivingP2p = false }
+ }
+ const instrumented = new WeakSet<object>()
+ const instrument = () => {
+ const entry = inspected.entries.get(machineId)
+ if (!entry) return
+ if (!instrumented.has(entry.ws)) {
+ instrumented.add(entry.ws)
+ const send = entry.ws.send.bind(entry.ws)
+ entry.ws.send = ((data: any, ...args: any[]) => {
+ if (typeof data !== 'string') sent.relay++
+ return (send as any)(data, ...args)
+ }) as typeof entry.ws.send
+ }
+ if (entry.p2p && !instrumented.has(entry.p2p)) {
+ instrumented.add(entry.p2p)
+ const send = entry.p2p.send.bind(entry.p2p)
+ entry.p2p.send = (data: any) => {
+ const ok = send(data)
+ if (ok && typeof data !== 'string') sent.p2p++
+ return ok
+ }
+ }
+ }
+ const snapshot = (stream: string): RouteSnapshot => {
+ instrument()
+ const entry = inspected.entries.get(machineId)
+ const ready = entry?.p2p?.isReady === true
+ const migrating = entry?.p2pMigrating.has(stream) ?? false
+ let pair: RouteSnapshot['pair'] = null
+ if (ready) {
+ for (const ice of entry!.p2p.pc?.iceTransports ?? []) {
+ if (!ice.connection?.candidatePairs?.some((p: any) => p.nominated)) continue
+ const selected = ice.getSelectedCandidatePair?.()
+ const local = candidateSummary(selected?.local?.candidate)
+ const remote = candidateSummary(selected?.remote?.candidate)
+ if (local && remote) { pair = { local, remote }; break }
+ }
+ }
+ return {
+ route: entry?.streams.has(stream) !== true ? 'unknown'
+ : verifiedRoute(entry?.p2pStreams.has(stream) ?? false, ready, migrating, pair),
+ ready, migrating, pair, sent: { ...sent }, received: { ...received },
+ }
+ }
+ server = new WebSocketServer({ host: '127.0.0.1', port: 0, path: '/api/local-ws' })
+ server.on('connection', (socket) => {
+ let session: RelaySession | undefined
+ let pending = Promise.resolve()
+ const sendFrame = (frame: unknown) => {
+ if (socket.readyState !== WebSocket.OPEN) return false
+ socket.send(JSON.stringify(frame)); return true
+ }
+ socket.on('error', () => {})
+ socket.on('close', () => { session?.detach() })
+ socket.on('message', (raw, binary) => {
+ pending = pending.then(async () => {
+ if (binary) {
+ const clear = decodeTerminalLocal(new Uint8Array(raw as Buffer))
+ if (!clear || !session) throw new Error('Invalid benchmark binary frame')
+ instrument()
+ await session.sendBinary(clear)
+ } else {
+ const frame = JSON.parse(raw.toString())
+ if (frame.type === 'machine_select' && !session) {
+ if (frame.payload.machineId !== machineId) throw new Error('Benchmark target mismatch')
+ session = await pool!.acquire(machineId, autonomousEnv, frame, {
+ sendFrame,
+ sendBinary: (bytes) => {
+ if (socket.readyState !== WebSocket.OPEN) return false
+ received[receivingP2p ? 'p2p' : 'relay']++
+ socket.send(bytes, { binary: true }); return true
+ },
+ }, () => socket.close(1011, 'Benchmark upstream closed'))
+ instrument()
+ if (socket.readyState !== WebSocket.OPEN) session.detach()
+ } else if (session) await session.send(frame)
+ else throw new Error('Select the benchmark machine first')
+ }
+ }).catch(() => { socket.close(1011, 'Benchmark transport failed') })
+ })
+ })
+ await new Promise<void>((resolve, reject) => { server!.once('listening', resolve); server!.once('error', reject) })
+ const address = server.address()
+ if (!address || typeof address === 'string') throw new Error('Missing benchmark loopback port')
+ return {
+ port: address.port,
+ snapshot,
+ negotiation: () => inspected.entries.get(machineId)?.p2p?.negotiationDetail ?? 'no data channel',
+ turnHosts: () => (inspected.entries.get(machineId)?.p2pPolicy?.turn?.urls ?? [])
+ .map((url) => url.match(/^turns?:([^:/?]+)/i)?.[1] ?? 'unrecognized'),
+ close: async () => {
+ for (const socket of server!.clients) socket.terminate()
+ pool!.invalidate(machineId)
+ await new Promise<void>((resolve) => server!.close(() => resolve()))
+ rmSync(scratch, { recursive: true, force: true })
+ },
+ }
+ } catch (error) {
+ pool?.invalidate(machineId)
+ server?.close()
+ rmSync(scratch, { recursive: true, force: true })
+ throw error
+ }
+}
diff --git a/cli/scripts/benchmark-terminal-latency.md b/cli/scripts/benchmark-terminal-latency.md
index 71ad574a2..bcb30026e 100644
--- a/cli/scripts/benchmark-terminal-latency.md
+++ b/cli/scripts/benchmark-terminal-latency.md
@@ -60,7 +60,65 @@ failure is a failed run; remove only the recorded probe, never unrelated agents.
For validation:
```sh
-npx tsc --noEmit --module nodenext --moduleResolution nodenext \
- --target es2022 --allowImportingTsExtensions --skipLibCheck \
- scripts/benchmark-terminal-latency.mts
+node --import tsx --test scripts/benchmark-route.test.ts
+npx tsc --noEmit --strict --module nodenext --moduleResolution nodenext \
+ --target es2023 --allowImportingTsExtensions --skipLibCheck \
+ scripts/benchmark-terminal-latency.mts scripts/benchmark-route.test.ts
```
+
+## Compare the three remote routes
+
+Add `--route p2p`, `--route turn`, or `--route relay` to opt into an **isolated
+source-built transport client**. Use the same remote machine, payload, sample
+count, and checkout for all three. Rotate route order between repetitions. The
+ordinary mode above continues to measure the installed daemon's automatic route.
+
+```sh
+npx tsx scripts/benchmark-terminal-latency.mts \
+ --machine MACHINE_ID --label office --route turn \
+ --samples 200 --control-samples 30 --revision SOURCE_COMMIT \
+ --output /private/tmp/office-turn-1.json
+```
+
+This mode hosts an ephemeral loopback WebSocket and the production
+`RemoteRelayPool` in the benchmark process. The loopback client, E2EE, wire format,
+remote daemon, and PTY remain real. The installed local daemon is not on the
+measured path; its version is recorded only as environment context. This shares
+one Node event loop between client and transport, so compare these three modes
+against each other, not as a before/after of the older installed-daemon results.
+
+- `p2p`: production negotiation with TURN credentials removed from this isolated
+ connection's offer policy, accepted only if the nominated ICE pair
+ has no relay candidate on either side. TURN fallback is an unavailable direct
+ trial, not a P2P result. This measures an available direct path, not the
+ production automatic route-selection success rate.
+- `turn`: the existing `TERMINAL_P2P_FORCE_RELAY` diagnostic flag is set only in
+ this process. A nominated relay candidate and Cloudflare policy hostnames are
+ required. The flag alone is not proof that traffic used TURN.
+- `relay`: production pool option `p2p: false` keeps terminal traffic on the
+ Harness backend WebSocket, including across automatic retry scheduling.
+
+Each run waits at most 35 seconds after probe readiness for the requested route.
+Every typing observation records a nominated candidate pair with addresses and
+ports removed, stream membership/migration state, and counters for actual binary
+sends and deliveries. It must have one input send and a matching PTY response on
+the expected path, with no opposite-path binary traffic during the observation.
+Missing candidate evidence, fallback, and mixed-route observations fail the run;
+retain them separately from the successful latency distributions. This checks
+one-at-a-time probe delivery, not arbitrary exactly-once semantics across outages.
+
+The adapter inspects the pinned production/werift internals and wraps send and
+delivery methods on its own pool for counters. Route restrictions apply only to
+this isolated connection; the installed daemon and production source are unchanged.
+Revalidate that instrumentation when upgrading those implementations. No candidate
+addresses, access tokens, TURN credentials, or private keys belong in published
+results. Console logs may identify machines: keep logs private and review failed
+result diagnostics before publishing.
+
+Authentication and the existing machine identity/pin are consumed read-only.
+Trust removal on this isolated connection affects only an in-memory snapshot.
+No key is generated or copied to disk. A scratch data directory prevents legacy
+state migration, and an expired login fails rather than refreshing or clearing
+the account. The active desktop's transport pool and sessions are never changed.
+Cleanup removes the one created terminal, closes the isolated pool and listener,
+and deletes the scratch directory before ending the process.
diff --git a/cli/scripts/benchmark-terminal-latency.mts b/cli/scripts/benchmark-terminal-latency.mts
index ea26e8210..31f9a8b28 100644
--- a/cli/scripts/benchmark-terminal-latency.mts
+++ b/cli/scripts/benchmark-terminal-latency.mts
@@ -10,6 +10,7 @@ import { deflateSync, inflateSync } from 'node:zlib'
import { performance } from 'node:perf_hooks'
import { parseArgs } from 'node:util'
import { decodeTerminalLocal, encodeTerminalLocal, TerminalBinaryKind } from '../src/lib/terminalBinary.ts'
+import { createBenchmarkRoute, verifyExchange, type BenchmarkRoute } from './benchmark-route.js'
const { values } = parseArgs({ options: {
machine: { type: 'string' }, label: { type: 'string', default: 'target' },
@@ -18,11 +19,16 @@ const { values } = parseArgs({ options: {
local: { type: 'boolean', default: false },
'control-samples': { type: 'string', default: '30' },
revision: { type: 'string' },
+ route: { type: 'string' },
} })
if (!values.machine || !values.output) throw new Error('--machine and --output are required')
const sampleCount = Number(values.samples)
const controlCount = Number(values['control-samples'])
-const port = Number(values.port)
+const daemonPort = Number(values.port)
+let port = daemonPort
+const requestedRoute = values.route as BenchmarkRoute | undefined
+if (requestedRoute && !['p2p', 'turn', 'relay'].includes(requestedRoute)) throw new Error('route must be p2p, turn, or relay')
+if (requestedRoute && values.local) throw new Error('--route requires a remote machine')
if (!Number.isInteger(sampleCount) || sampleCount < 1 || sampleCount > 2000) throw new Error('samples must be 1–2000')
if (!Number.isInteger(controlCount) || controlCount < 1 || controlCount > 500) throw new Error('control-samples must be 1–500')
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('invalid port')
@@ -36,7 +42,7 @@ const prefix = `~HB${nonce}:`
const rows: Record<string, unknown>[] = []
const controls: Record<string, unknown>[] = []
const result: Record<string, any> = {
- schema: 3, success: false, startedAt: new Date().toISOString(), label: values.label,
+ schema: requestedRoute ? 4 : 3, success: false, startedAt: new Date().toISOString(), label: values.label,
sourceRevision: values.revision ?? 'unspecified',
boundary: 'client binary input send to matching PTY probe response received; excludes UI rendering and OS keyboard input',
client: { platform: platform(), arch: arch(), node: process.version },
@@ -44,6 +50,7 @@ const result: Record<string, any> = {
controlSamples: controlCount, controlObservations: controls,
observations: rows, stagesMs: {}, cleanup: { created: false, deleted: false },
linkModes: [],
+ ...(requestedRoute ? { requestedRoute, routeVerification: 'nominated ICE pair, stream membership, and both binary wire directions for each echo' } : {}),
}
class Peer {
@@ -238,6 +245,11 @@ finally:
done.set()
termios.tcsetattr(fd,termios.TCSADRAIN,saved)
`
+const routeAdapter = requestedRoute ? await createBenchmarkRoute(values.machine, requestedRoute) : undefined
+if (routeAdapter) {
+ port = routeAdapter.port
+ result.client.transportImplementation = 'isolated production RemoteRelayPool from source; loopback client and relay share one Node process'
+}
let peer = new Peer()
let agentId: string | undefined
let echoSeq = 0
@@ -245,11 +257,12 @@ const interrupted = () => peer.fail(new Error('benchmark interrupted; attempting
process.once('SIGINT', interrupted)
process.once('SIGTERM', interrupted)
try {
- const status = await fetch(`http://127.0.0.1:${port}/api/status`, { signal: AbortSignal.timeout(5000) }).then((r) => r.json()) as { version?: string }
- result.client.daemonVersion = status.version ?? 'unknown'
+ const status = await fetch(`http://127.0.0.1:${daemonPort}/api/status`, { signal: AbortSignal.timeout(5000) }).then((r) => r.json()) as { version?: string }
+ result.client[routeAdapter ? 'installedDaemonVersionNotOnMeasuredPath' : 'daemonVersion'] = status.version ?? 'unknown'
const connectionBegan = now()
result.stagesMs.select = await peer.select()
- await peer.rpc('terminal_capabilities', {})
+ const capabilities = await peer.rpc('terminal_capabilities', {})
+ result.targetProtocolVersion = capabilities.protocolVersion ?? null
result.stagesMs.readyForRequests = now() - connectionBegan
const began = now()
const created = await peer.rpc('agent_create', { engine: 'terminal', name: `Performance probe ${nonce.slice(0, 8)}`, cwd: '/tmp', bypassPermission: false })
@@ -261,6 +274,19 @@ try {
const command = `python3 -u -c 'import base64,zlib;exec(zlib.decompress(base64.b64decode("${deflateSync(Buffer.from(probe)).toString('base64')}")))'`
result.stagesMs.startProbe = await peer.exchange(Buffer.from(command), prefix + 'READY:', 15000, true)
result.target = peer.output.match(new RegExp(`${prefix}READY:([^~]+)~`))?.[1] ?? 'unknown'
+ if (routeAdapter) {
+ const began = now()
+ while (routeAdapter.snapshot(peer.stream).route !== requestedRoute && now() - began < 35000) await sleep(100)
+ result.stagesMs.waitForRequestedRoute = now() - began
+ result.routeAtStart = routeAdapter.snapshot(peer.stream)
+ result.negotiation = routeAdapter.negotiation()
+ // Public service host names only. No TURN credentials or candidate addresses.
+ result.turnHosts = [...new Set(routeAdapter.turnHosts())]
+ if (result.routeAtStart.route !== requestedRoute) throw new Error(`Requested ${requestedRoute} unavailable; actual route ${result.routeAtStart.route}`)
+ if (requestedRoute === 'turn' && (!result.turnHosts.length || result.turnHosts.some((host: string) => !/(^|\.)cloudflare\.com$/.test(host)))) {
+ throw new Error('Selected TURN pair cannot be attributed to Cloudflare configuration')
+ }
+ }
// Permit normal direct-path negotiation; record actual modes on every observation.
await sleep(2000)
// Use the application's normal streamless capability request. Adding a
@@ -289,9 +315,26 @@ try {
const byte = 97 + echoSeq % 26
const modeBefore = peer.mode
const keyframesBefore = peer.keyframes
- const elapsed = await peer.exchange(Buffer.from([byte]), `${prefix}ECHO:${echoSeq}:${byte}~`)
- rows.push({ load, phase: sample < 0 ? 'warmup' : 'measured', sequence: echoSeq++, elapsedMs: elapsed,
- modeBefore, modeAfter: peer.mode, keyframesDuring: peer.keyframes - keyframesBefore })
+ const routeBefore = routeAdapter?.snapshot(peer.stream)
+ const began = now()
+ let rowRecorded = false
+ try {
+ const elapsed = await peer.exchange(Buffer.from([byte]), `${prefix}ECHO:${echoSeq}:${byte}~`)
+ const routeAfter = routeAdapter?.snapshot(peer.stream)
+ const routeVerified = !requestedRoute || verifyExchange(requestedRoute, routeBefore!, routeAfter!)
+ rows.push({ load, phase: sample < 0 ? 'warmup' : 'measured', sequence: echoSeq++, elapsedMs: elapsed,
+ success: routeVerified, modeBefore, modeAfter: peer.mode, keyframesDuring: peer.keyframes - keyframesBefore,
+ ...(routeAdapter ? { routeBefore, routeAfter, routeVerified } : {}) })
+ rowRecorded = true
+ if (!routeVerified) throw new Error('Route changed or binary wire verification failed; run is excluded')
+ } catch (error) {
+ if (!rowRecorded) {
+ rows.push({ load, phase: sample < 0 ? 'warmup' : 'measured', sequence: echoSeq,
+ success: false, elapsedMs: now() - began, error: String(error), modeBefore, modeAfter: peer.mode,
+ ...(routeAdapter ? { routeBefore, routeAfter: routeAdapter.snapshot(peer.stream), routeVerified: false } : {}) })
+ }
+ throw error
+ }
}
result[`${load}Achieved`] = { bytes: peer.outputBytes - bytesBefore, durationMs: now() - loadBegan }
if (load !== 'idle') await peer.exchange(Buffer.from([1]), `${prefix}LOAD:0~`)
@@ -331,11 +374,13 @@ try {
}
}
peer.close()
+ if (routeAdapter) await routeAdapter.close()
result.finishedAt = new Date().toISOString()
result.summaries = ['idle', 'redraw_20hz'].map((load) => {
- const values = rows.filter((row) => row.load === load && row.phase === 'measured').map((row) => Number(row.elapsedMs)).sort((a,b) => a-b)
+ const values = rows.filter((row) => row.load === load && row.phase === 'measured' && row.success !== false).map((row) => Number(row.elapsedMs)).sort((a,b) => a-b)
const at = (p: number) => values.length ? values[Math.ceil(values.length * p) - 1] : null
- return { load, samples: values.length, p50Ms: at(.5), p95Ms: at(.95), p99Ms: at(.99), maxMs: values.at(-1) ?? null }
+ return { load, samples: values.length, failures: rows.filter((row) => row.load === load && row.phase === 'measured' && row.success === false).length,
+ p50Ms: at(.5), p95Ms: at(.95), p99Ms: at(.99), maxMs: values.at(-1) ?? null }
})
result.controlSummaries = ['machine'].map((route) => {
const values = controls.filter((row) => row.route === route && row.phase === 'measured' && row.success === true).map((row) => Number(row.elapsedMs)).sort((a,b) => a-b)
@@ -345,7 +390,7 @@ try {
})
result.summariesByTerminalMode = [...new Set(rows.map((row) => `${row.load}/${row.modeBefore === row.modeAfter ? row.modeAfter : 'changed_during_sample'}`))].map((group) => {
const [load, mode] = group.split('/')
- const values = rows.filter((row) => row.phase === 'measured' && row.load === load
+ const values = rows.filter((row) => row.phase === 'measured' && row.success !== false && row.load === load
&& (row.modeBefore === row.modeAfter ? row.modeAfter : 'changed_during_sample') === mode)
.map((row) => Number(row.elapsedMs)).sort((a,b) => a-b)
const at = (p: number) => values.length ? values[Math.ceil(values.length * p) - 1] : null
@@ -357,3 +402,6 @@ try {
console.log(JSON.stringify({ label: values.label, success: result.success, summaries: result.summaries, stagesMs: result.stagesMs,
controlSummaries: result.controlSummaries, reattach: result.reattach, target: result.target, cleanup: result.cleanup, error: result.error }))
}
+// werift can retain STUN retry timers after its peer has closed. All protocol
+// cleanup and synchronous artifact writing above finish before this process exits.
+if (routeAdapter) process.exit(process.exitCode ?? 0)