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
import Fastify from 'fastify'
import { expect, it } from 'vitest'
import { createRouteLatency, markNothingNew, registerRouteLatency } from './routeLatency.js'
type Logged = { message: string; context: Record<string, unknown> }
type Route = { n: number; p50: number; p95: number; max: number }
function latency(options: Parameters<typeof createRouteLatency>[0] = {}) {
const logs: Logged[] = []
const l = createRouteLatency({ pod: 'pod-1', log: (message, context) => logs.push({ message, context }), ...options })
return { l, logs, routes: (i = 0) => logs[i]!.context.routes as Record<string, Route> }
}
it('logs each route p50 and p95 for the window, then starts a new one', () => {
const { l, logs, routes } = latency()
for (let ms = 1; ms <= 100; ms++) l.record('GET /api/items/:id', ms)
l.record('POST /api/items', 7)
l.flush()
expect(logs[0]!.message).toBe('route latency')
expect(logs[0]!.context).toMatchObject({ pod: 'pod-1', requests: 101 })
expect(routes()).toEqual({ 'GET /api/items/:id': { n: 100, p50: 50, p95: 95, max: 100 }, 'POST /api/items': { n: 1, p50: 7, p95: 7, max: 7 } })
l.flush()
expect(logs[1]!.context).toMatchObject({ requests: 0, routes: {} })
})
it('keeps a bounded sample per route but counts every request', () => {
const { l, routes } = latency({ maxSamples: 10, random: () => 0.99 })
for (let i = 0; i < 1000; i++) l.record('GET /api/x', i < 10 ? 3 : 900)
l.flush()
// A random draw of 0.99 never replaces one of the first 10 samples: the percentiles come from them, the max from all.
expect(routes()['GET /api/x']).toEqual({ n: 1000, p50: 3, p95: 3, max: 900 })
})
it('keeps a bounded number of routes and counts the rest under other', () => {
const { l, routes } = latency({ maxRoutes: 2 })
for (const route of ['GET /a', 'GET /b', 'GET /c', 'GET /d']) l.record(route, 1)
l.flush()
expect(routes()).toEqual({ 'GET /a': { n: 1, p50: 1, p95: 1, max: 1 }, other: { n: 3, p50: 1, p95: 1, max: 1 } })
})
it('lists the routes that took the most time first, up to top', () => {
const { l, routes } = latency({ top: 2 })
l.record('GET /rare-slow', 500)
for (let i = 0; i < 100; i++) l.record('GET /often-fast', 10)
l.record('GET /rare-fast', 1)
l.flush()
expect(Object.keys(routes())).toEqual(['GET /often-fast', 'GET /rare-slow'])
})
it('names each request by its route pattern, never its URL', async () => {
const app = Fastify()
const logs: Logged[] = []
const l = registerRouteLatency(app, { pod: 'pod-1', log: (message, context) => logs.push({ message, context }) })
app.get('/api/items/:id', async () => ({ ok: true }))
for (const id of ['alice-secret-1', 'bob-2']) await app.inject({ method: 'GET', url: `/api/items/${id}?token=hunter2` })
await app.inject({ method: 'GET', url: '/nowhere' })
l.flush()
const routes = logs[0]!.context.routes as Record<string, Route>
expect(Object.keys(routes).sort()).toEqual(['GET /api/items/:id', 'GET unrouted'])
expect(routes['GET /api/items/:id']!.n).toBe(2)
expect(routes['GET /api/items/:id']!.p95).toBeGreaterThanOrEqual(0)
expect(JSON.stringify(logs)).not.toMatch(/alice|bob|hunter2|nowhere/)
await app.close()
})
it('counts each route by kind of client, from its user agent and whether it reads as an agent, never the agent itself', async () => {
const app = Fastify()
const logs: Logged[] = []
const l = registerRouteLatency(app, { pod: 'pod-1', log: (message, context) => logs.push({ message, context }) })
app.get('/api/items', async () => ({ ok: true }))
const as = (ua: string, url = '/api/items') => app.inject({ method: 'GET', url, headers: { 'user-agent': ua } })
await as('Mozilla/5.0 (Macintosh) Chrome/130')
await as('Dart/3.5 (dart:io)')
await as('Dart/3.5 (dart:io)')
await as('node')
await as('undici', '/api/items?asAgentId=agent-secret-7')
await as('curl/8.4')
l.flush()
expect((logs[0]!.context.routes as Record<string, { clients: Record<string, number> }>)['GET /api/items']!.clients)
.toEqual({ web: 1, app: 2, cli: 1, agent: 1, other: 1 })
expect(JSON.stringify(logs)).not.toMatch(/agent-secret|curl|Chrome/)
await app.close()
})
it('a reply marked nothing-new counts as empty, and bytes are summed per route (spec 2026-11-02 poll cost, Part 0)', async () => {
const logs: Logged[] = []
const app = Fastify()
const l = registerRouteLatency(app, { pod: 'p', log: (message, context) => logs.push({ message, context }) })
app.get('/poll/:id', async (request) => {
if ((request.params as { id: string }).id === 'none') markNothingNew(request)
return { rows: (request.params as { id: string }).id === 'none' ? [] : ['abc'] }
})
await app.inject('/poll/none'); await app.inject('/poll/none'); await app.inject('/poll/some')
l.flush()
const route = (logs[0]!.context.routes as Record<string, { n: number; empty?: number; bytes?: number }>)['GET /poll/:id']!
expect(route.n).toBe(3)
expect(route.empty).toBe(2)
expect(route.bytes).toBe(2 * '{"rows":[]}'.length + '{"rows":["abc"]}'.length)
await app.close()
})
it('leaves empty and bytes out of a route that has none', () => {
const { l, routes } = latency()
l.record('GET /a', 1)
l.flush()
expect(routes()['GET /a']).toEqual({ n: 1, p50: 1, p95: 1, max: 1 })
})
it('hands each response to the metrics from the same hook, by pattern and status, so a request is timed once (review of #1880)', async () => {
const seen: Array<[string, string, number, number]> = []
const app = Fastify()
registerRouteLatency(app, { pod: 'p', log: () => {}, observe: (method, route, status, seconds) => { seen.push([method, route, status, seconds]) } })
app.get('/api/things/:id', async () => ({ ok: true }))
await app.ready()
await app.inject({ method: 'GET', url: '/api/things/6a730145e13f6277cdf98c1a?token=secret-value' })
await app.inject({ method: 'GET', url: '/wp-admin/6a730145e13f6277cdf98c1a' })
await app.close()
expect(seen.map(([method, route, status]) => [method, route, status])).toEqual([['GET', '/api/things/:id', 200], ['GET', 'unrouted', 404]])
for (const [, , , seconds] of seen) expect(seconds).toBeGreaterThanOrEqual(0)
expect(JSON.stringify(seen)).not.toContain('6a730145e13f6277cdf98c1a')
})